gpt4 book ai didi

Python:如何从 Pandas 图中叠加 2 个条形图

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:05 26 4
gpt4 key购买 nike

我想用相同的 x-xis 叠加 2 个条形图。我有一个包含此数据的数据框:

df = pd.DataFrame({'a':[1,2,3,1,2,2,2],
'b':[1,1,1,3,2,2,2]})

我可以为 a 和 b 绘制 2 个直方图:

df['a'].value_counts().plot(kind = 'bar')
df['b'].value_counts().plot(kind = 'bar')

enter image description here enter image description here

如何将其中一个叠加到另一个上?

我正在寻找的不相关图表格式示例: enter image description here

最佳答案

使用widthax 参数:

ax = df['a'].value_counts().plot(kind='bar', color='blue', width=.75, legend=True, alpha=0.8)
df['b'].value_counts().plot(kind='bar', color='maroon', width=.5, alpha=1, legend=True)

输出:

enter image description here

关于Python:如何从 Pandas 图中叠加 2 个条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com