堆叠的条形图Matplotlib或Seaborn

发布于 2025-02-07 03:57:18 字数 383 浏览 1 评论 0原文

我有以下数据框:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2025-02-14 03:57:18

您可以使用 使用stacked = true

data.pivot('year', 'country', 'share').plot.bar(stacked=True)

output:

”在此处输入图像说明”

You can use a pivot and plot.bar with stacked=True:

data.pivot('year', 'country', 'share').plot.bar(stacked=True)

output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文