我希望使用
考虑以下简单示例:
现在的图表如下:
ax.vlines
创建的竖线能够像axvline
一样与图表的上下边界相接触。考虑以下简单示例:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'time' : [pd.to_datetime('2016-01-02'), pd.to_datetime('2016-01-03'), pd.to_datetime('2016-01-04')],
'value1' : [1, 2, 3],
'value2' : [10, 20, 30]})
df.set_index('time', inplace = True)
df
Out[95]:
value1 value2
time
2016-01-02 1 10
2016-01-03 2 20
2016-01-04 3 30
现在的图表如下:
fig, ax = plt.subplots(figsize=(30, 15))
ax.plot(df.index, df["value1"], color = 'black')
ax2 = ax.twinx()
ax2.plot(df.index, df["value2"], color = 'red')
#axvline stretches nicely
ax.axvline( pd.to_datetime('2016-01-04'), color = 'red', alpha = 1, linestyle = '--')
#vlines stops before touching the upper and lower boundaries
ymin, ymax = ax2.get_ylim()
ax.vlines([pd.to_datetime('2016-01-02'), pd.to_datetime('2016-01-03')], ymin = ymin, ymax = ymax, color = 'blue', linestyle = '-')
提供的内容:
您可以看到那些蓝色线条仍然悬浮在空中,而漂亮的红色垂直线则完全延伸。我该如何实现这一点?
谢谢!
axvline
的作用相同,因为它在垂直方向上使用轴坐标。 - ImportanceOfBeingErnest