【问题标题】:matplotlib - plotting a straight linematplotlib - 绘制一条直线
【发布时间】:2020-07-03 17:10:38
【问题描述】:

鉴于以下代码部分:

    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)

虽然我无法绘制线。这样做的最佳方法是什么?

【问题讨论】:

    标签: python python-3.x pandas matplotlib annotations


    【解决方案1】:

    问题是num.lt 返回一个系列,而axvline 想要一个标量。

    尝试循环并为每个索引值画一条线:

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

    【讨论】:

    • 感谢您的帮助,我了解您的解决方案。我收到以下错误:AttributeError: 'Line2D' object has no property 'ax' Without ax 我怎么知道应该在哪里绘制?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    相关资源
    最近更新 更多