熊猫线图与不同的颜色

时间:2017-07-20 14:48:07

标签: python-3.x pandas matplotlib dataframe

我有以下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中读到了类似的问题,但我无法实现它。

1 个答案:

答案 0 :(得分: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()