【问题标题】:Pandas line plot with different colors不同颜色的熊猫线图
【发布时间】:2017-07-20 14:48:06
【问题描述】:

我有以下 pandas 数据框,我想要一个线图,前 10 个点的颜色与其他点不同。

df = [

    A           B  
2017-05-01    31.98
2017-05-02    31.
2017-05-03    30.41
2017-05-04    29.
2017-05-05    29.
2017-05-06    28.48
2017-05-07    27.73
2017-05-08    26.
2017-05-09    26.29
2017-05-10    27.
2017-05-11    28.56
2017-05-12    30.
2017-05-13    31.66
2017-05-14    33.
2017-05-15    35.67
2017-05-16    38.
2017-05-17    40.59
2017-05-18    43.
2017-05-19    46.42
2017-05-20    49.68
2017-05-21    53.
]

我尝试在 matplotlib 中这样做,比如

plt.plot(df[B][:11])
plt.plot(df[B][11:])
plt.show()

但我在一个图中有两条不同的线。相反,我想要一条具有两种不同颜色的连续线。我在this link 中阅读了类似的问题,但我无法实现。

【问题讨论】:

    标签: python-3.x pandas matplotlib dataframe


    【解决方案1】:

    我想这就是你想要的:

    df.A = pd.to_datetime(df.A)
    
    plt.plot(df.A[:11], df.B[:11])
    plt.plot(df.A[10:], df.B[10:])
    
    plt.xticks(rotation=45)
    
    plt.show()
    

    【讨论】:

    • 没错,但我的 X 轴是日期时间而不是数字。
    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2018-11-30
    • 2019-06-18
    • 2015-09-24
    相关资源
    最近更新 更多