我有一个有三列Date
(时间戳),Color
('红'或'蓝')和Value
(int)的熊猫数据框。seaborn或matplotlib折线图,线颜色取决于变量
我目前得到一个线图,从它与下面的代码:
import matplotlib.pyplot as plt
import pandas as pd
Dates=['01/01/2014','02/01/2014','03/01/2014','04/01/2014','05/01/2014','06/01/2014','07/01/2014']
Values=[3,4,6,5,4,5,4]
Colors=['red','red','blue','blue','blue','red','red']
df=pd.DataFrame({'Dates':Dates,'Values':Values,'Colors':Colors})
df['Dates']=pd.to_datetime(df['Dates'],dayfirst=True)
grouped = df.groupby('Colors')
fig, ax = plt.subplots()
for key, group in grouped:
group.plot(ax=ax, x="Dates", y="Values", label=key, color=key)
plt.show()
我想的线条颜色依赖于“色”列。我怎样才能做到这一点?
我看过here散点图的一个类似的问题,但似乎我不能将相同的解决方案应用于时间序列折线图。目前
我的输出是这样的:
我想正如我说你能达到这样的(只有一条线,但几种颜色)
可能的复制[绘制不同的颜色使用matplotlib不同的分类级别(http://stackoverflow.com/questions/26139423/plot-different- color-for-different-categorical-levels-using-matplotlib) –
你可以附加你的数据框的一部分吗? –
刚刚添加它:-) –