堆叠的条形图Matplotlib或Seaborn
我有以下数据框:
import pandas as pd
data = {'country': ['US', 'DE', 'IT', 'US', 'DE', 'IT', 'US', 'DE', 'IT'],
'year': [2000,2000,2000,2001,2001,2001,2002,2002,2002],
'share': [0.5, 0.3, 0.2, 0.6,0.1,0.3,0.4,0.2,0.4]}
data = pd.DataFrame(data)
我想用堆叠的条形图显示数据。
X轴:一年, Y轴:分享, 颜色:
2000、2001和2002年的所有三个条应具有相同的高度(每年的份额== 1)
I have the following dataframe:
import pandas as pd
data = {'country': ['US', 'DE', 'IT', 'US', 'DE', 'IT', 'US', 'DE', 'IT'],
'year': [2000,2000,2000,2001,2001,2001,2002,2002,2002],
'share': [0.5, 0.3, 0.2, 0.6,0.1,0.3,0.4,0.2,0.4]}
data = pd.DataFrame(data)
I want to display the data with a stacked bar chart.
X-axis: year,
Y-axis: share,
Color: country
All the three bars for 2000, 2001 and 2002 should have the same height (for each year, the total of the share == 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用和 使用
stacked = true
:output:
You can use a
pivot
andplot.bar
withstacked=True
:output: