[英]Plot histogram using Python with different colors for positive and negative values
[英]How to plot a histogram with different colors by group and facet grid in python using matplotlib
我有以下数据:
import pandas as pd
data = pd.DataFrame({"group": ["aa", "aa", "aa", "aa", "bb", "bb", "bb", "bb"],
"segment": ["da", "et", "da", "et", "da", "et", "da", "et"],
"country": ["br", "br", "th", "th", "br", "br", "th", "th"],
"N": [31, 23, 17, 9, 4, 100, 10, 20],
"totalN": [84, 84, 389, 389, 84, 84, 389, 389]}
)
我想在python中进行相同的绘图,例如以下R代码产生的绘图
ggplot(data, aes(x=segment, y=N, fill=group)) +
geom_bar(stat="identity") +
ggtitle("group") +
facet_grid(country~.)+
geom_text(aes(label=percent(round(N / totalN, 2))), position=position_stack(vjust=0.5), size=3) +
coord_flip()
我努力了
data_groupped = data.groupby(['group', 'segment'])
data_groupped.plot(x='segment', y='N', kind='hist')
它分别生成每个直方图。
因此,预期的输出是这样的:
使用熊猫图,您可以
选项1]使用pivot_table
重塑groups
数据
import matplotlib.pyplot as plt
groups = data.groupby('country')
fig, axes = plt.subplots(groups.ngroups,sharex=True)
for (g, grp), ax in zip(groups, axes.flatten()):
grp_df = grp.pivot_table(index='segment', columns='group', values='N', aggfunc=np.sum)
grp_df.plot.barh(stacked=True, ax=ax, sharex=True)
选项2]首先将数据重塑为df
,然后使用plot
df = (data.groupby('country')
.apply(lambda x: x.groupby(['segment', 'group'])['N'].sum().unstack())
.unstack(level=0)
.reorder_levels((1,0), axis=1)
.sort_index(axis=1)
)
cgroups = df.groupby(level=0, axis=1)
fig, axes = plt.subplots(cgroups.ngroups, sharex=True)
for (c, grp), ax in zip(cgroups, axes.flatten()):
sp = grp[c].plot.barh(stacked=True, ax=ax, sharex=True)
df
选项3]如果不需要分离子图
df = (data.groupby('country')
.apply(lambda x: x.groupby(['segment', 'group'])['N'].sum().unstack()))
df.plot.barh(stacked=True)
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.