matplotlib - 绘制一条直线
matplotlib - plotting a straight line
给定以下代码部分:
fig, (ax1, ax2) = plt.subplots(2, 1)
num = sto_list.gt(70).sum(1)
plt.yticks(fontsize = 25)
df2 = web.DataReader('fb', 'yahoo', start, end)
ax = num.plot(figsize=(45,25), ax=ax2, color = 'Red')
df2.plot(y = 'Close', figsize=(45,25), ax=ax1, color = 'Green')
ax.grid()
ax1.xaxis.label.set_visible(False)
ax.xaxis.label.set_visible(False)
这会生成如下所示的图表:
底部的子图是从 num:
绘制的
num
Out[70]:
Date
2015-07-06 33
2015-07-07 20
2015-07-08 4
2015-07-09 8
2015-07-10 8
..
2020-06-29 14
2020-06-30 13
2020-07-01 18
2020-07-02 20
2020-07-03 28
Length: 1228, dtype: int64
我想做的是在小于 10 的地方绘制一条直线:
plt.axvline(x=num.lt(10), ax = ax2)
虽然我无法绘制这条线。这样做的最佳方式是什么?
问题是 num.lt
returns 一个系列,axvline
想要一个标量。
尝试循环并为每个索引值画一条线:
dates = num[num.lt(10)].index
for d in dates:
ax2.axvline(d)
给定以下代码部分:
fig, (ax1, ax2) = plt.subplots(2, 1)
num = sto_list.gt(70).sum(1)
plt.yticks(fontsize = 25)
df2 = web.DataReader('fb', 'yahoo', start, end)
ax = num.plot(figsize=(45,25), ax=ax2, color = 'Red')
df2.plot(y = 'Close', figsize=(45,25), ax=ax1, color = 'Green')
ax.grid()
ax1.xaxis.label.set_visible(False)
ax.xaxis.label.set_visible(False)
这会生成如下所示的图表:
底部的子图是从 num:
绘制的num
Out[70]:
Date
2015-07-06 33
2015-07-07 20
2015-07-08 4
2015-07-09 8
2015-07-10 8
..
2020-06-29 14
2020-06-30 13
2020-07-01 18
2020-07-02 20
2020-07-03 28
Length: 1228, dtype: int64
我想做的是在小于 10 的地方绘制一条直线:
plt.axvline(x=num.lt(10), ax = ax2)
虽然我无法绘制这条线。这样做的最佳方式是什么?
问题是 num.lt
returns 一个系列,axvline
想要一个标量。
尝试循环并为每个索引值画一条线:
dates = num[num.lt(10)].index
for d in dates:
ax2.axvline(d)