我有一个包含3个变量的数据框:
data= [["2019/oct",10,"Approved"],["2019/oct",20,"Approved"],["2019/oct",30,"Approved"],["2019/oct",40,"Approved"],["2019/nov",20,"Under evaluation"],["2019/dec",30,"Aproved"]]
df = pd.DataFrame(data, columns=['Period', 'Observations', 'Result'])
我想要一个按期间列分组的条形图,显示观察值列中包含的所有值并用结果列着色。 我该怎么办?
我尝试了sns.barplot,但它仅在一个小节(值的平均值)中将“观察值”列中的值连接在一起。
sns.barplot(x='Period',y='Observations',hue='Result',data=df,ci=None)
答案 0 :(得分:1)
假设每行需要一个条形,则可以执行以下操作:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
result_cat = df["Result"].astype("category")
result_codes = result_cat.cat.codes.values
cmap = plt.cm.Dark2(range(df["Result"].unique().shape[0]))
patches = []
for code in result_cat.cat.codes.unique():
cat = result_cat.cat.categories[code]
patches.append(mpatches.Patch(color=cmap[code], label=cat))
df.plot.bar(x='Period',
y='Observations',
color=cmap[result_codes],
legend=False)
plt.ylabel("Observations")
plt.legend(handles=patches)
答案 1 :(得分:1)
如果您希望按月分组,然后堆叠,请使用以下内容(请注意,我已更新您的代码以确保一个月中有多个状态),但不确定我是否完全正确理解了您的问题:< / p>
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
data= [["2019/oct",10,"Approved"],["2019/oct",20,"Approved"],["2019/oct",30,"Approved"],["2019/oct",40,"Under evaluation"],["2019/nov",20,"Under evaluation"],["2019/dec",30,"Aproved"]]
df = pd.DataFrame(data, columns=['Period', 'Observations', 'Result'])
df.groupby(['Period', 'Result'])['Observations'].sum().unstack('Result').plot(kind='bar', stacked=True)