简体   繁体   English

matplotlib - 绘制直线

[英]matplotlib - plotting a straight line

Given the following section of code:给定以下代码部分:

    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)

This produces a chart which looks like this:这会生成一个如下所示的图表: 在此处输入图像描述

The subplot at the bottom is plotted from num:底部的子图是从 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

What i want to do is plot a straight line wherever it is less than 10 with this:我想要做的是 plot 一条直线,只要它小于 10 就可以了:

plt.axvline(x=num.lt(10), ax = ax2)

I am not able to plot the line though.我无法 plot 这条线。 What would be the best way in doing so?这样做的最佳方法是什么?

The problem is that num.lt returns a series and axvline wants a scalar.问题是num.lt返回一个系列,而axvline需要一个标量。

Try looping through and drawing a line for each index value:尝试循环并为每个索引值画一条线:

dates = num[num.lt(10)].index
for d in dates:
    ax2.axvline(d)       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM