【发布时间】:2019-05-01 11:13:12
【问题描述】:
我在 csv 文件中有以下数据:
Date City TruckA TruckB TruckC TruckD
Date1 City1 1 0 0 0
Date1 City2 0 0 1 0
Date1 City3 1 0 0 0
Date1 City4 0 0 1 0
Date2 City1 1 0 0 0
Date2 City2 0 1 0 0
Date2 City3 0 0 0 1
Date2 City4 1 0 0 0
Date2 City5 0 1 0 0
Date3 City1 1 0 0 0
Date3 City2 0 0 1 0
Date3 City3 1 0 0 0
Date3 City4 0 0 1 0
我可以使用此代码成功绘制数据:
import pandas as pd
df = pd.read_csv("data.csv")
print(df)
df = df.set_index(["Date","City"])
df.unstack().plot(kind='bar', stacked=True)
我得到以下结果:
如您所见,颜色图例就像每一对 (City,Truck) 都有一个颜色。我希望图例仅依赖于卡车,并且理想情况下在每个城市的条形图上都有标签。
这可能吗?
【问题讨论】:
标签: python pandas dataframe matplotlib