不能在堆叠的条形图中绘制所有数据吗?

发布于 2025-01-30 23:41:36 字数 1494 浏览 1 评论 0原文

我尝试绘制水平堆叠的条形图,其高度为Y轴,欧洲和北美(数据)沿着X轴堆叠,并使用以下代码堆叠。不知道怎么了。并非所有数据都显示出来,条的宽度太薄而无法查看。

fig, ax = plt.subplots()
ax.barh(EleRegion['Elevation'], EleRegion['Europe'], label = 'Europe')
ax.barh(EleRegion['Elevation'], EleRegion['North America'], label ='North America' )

数据集在下面:

0        0
1      100
2      200
3      300
4      400
5      500
6      600
7      700
8      800
9      900
10    1000
11    1100
12    1200
13    1300
14    1400
15    1500
16    1600
17    1700
18    1800
19    1900
20    2000
21    2100
22    2200
23    2300
Name: Elevation, dtype: int64 0     11188
1      9407
2      8801
3      8801
4      8692
5      7563
6      7134
7      6121
8      5077
9      3684
10     3330
11     1084
12     1084
13      918
14      918
15      776
16      776
17       75
18       75d
19        0
20        0
21        0
22        0
23        0
Name: Europe, dtype: int64
0     2657
1     1786
2     1786
3     1786
4      959
5      885
6      564
7      187
8      187
9        0
10       0
11       0
12       0
13       0
14       0
15       0
16       0
17       0
18       0
19       0
20       0
21       0
22       0
23       0
Name: North America, dtype: int64

///重复发布///我尝试绘制水平堆叠的条形图,高程为y轴,沿着欧洲和北美(数据)沿带有以下代码的X轴。不知道怎么了。并非所有数据都显示出来,条的宽度太薄而无法查看。

I try to plot a horizontal stacked bar chart, with elevation as the y axis, and Europe and North America (data) stacked along the x-axis with the following code. Not sure what's wrong. Not all data are shown and the width of the bar is too thin to look at.

fig, ax = plt.subplots()
ax.barh(EleRegion['Elevation'], EleRegion['Europe'], label = 'Europe')
ax.barh(EleRegion['Elevation'], EleRegion['North America'], label ='North America' )

enter image description here

The dataset is below:

0        0
1      100
2      200
3      300
4      400
5      500
6      600
7      700
8      800
9      900
10    1000
11    1100
12    1200
13    1300
14    1400
15    1500
16    1600
17    1700
18    1800
19    1900
20    2000
21    2100
22    2200
23    2300
Name: Elevation, dtype: int64 0     11188
1      9407
2      8801
3      8801
4      8692
5      7563
6      7134
7      6121
8      5077
9      3684
10     3330
11     1084
12     1084
13      918
14      918
15      776
16      776
17       75
18       75d
19        0
20        0
21        0
22        0
23        0
Name: Europe, dtype: int64
0     2657
1     1786
2     1786
3     1786
4      959
5      885
6      564
7      187
8      187
9        0
10       0
11       0
12       0
13       0
14       0
15       0
16       0
17       0
18       0
19       0
20       0
21       0
22       0
23       0
Name: North America, dtype: int64

///repeat to post/// I try to plot a horizontal stacked bar chart, with elevation as the y axis, and Europe and North America (data) stacked along the x-axis with the following code. Not sure what's wrong. Not all data are shown and the width of the bar is too thin to look at.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

落花随流水 2025-02-06 23:41:36

您可以将高程设置为索引,并使用 dataframe.plot.barh

ax = EleRegion.set_index('Elevation').plot.barh(stacked=True)
ax.set_ylabel('Count')

结果:

原因不起作用:

有两个问题:

  • axes.barh 接受,<代码>宽度和高度。您正在传递前两个,但是高度仍然是默认0.8。问题在于您的y轴在数千个范围内,因此条太小了,无法看到。您可以将其设置为50,您会看到它们:
ax.barh(EleRegion['Elevation'],  EleRegion['Europe'], 50, label = 'Europe')

​ yticks 设置为正确的数字

  • )作为y,将是使用axes.barh创建的第二个条形图要求参数真正被“堆叠”,否则第二个图表仅在第一个图表的顶部呈现:
f, ax = plt.subplots()
ax.barh(EleRegion['Elevation'], EleRegion['Europe'], 50, label='Europe')
ax.barh(EleRegion['Elevation'], EleRegion['North America'], 50, left=EleRegion['Europe'], label='North America' )
plt.show()

这就是为什么,由于您的数据已经是dataframe,因此使用dataframe.plot.barh更方便地自动处理所有这些。

You can set Elevation as index and use DataFrame.plot.barh:

ax = EleRegion.set_index('Elevation').plot.barh(stacked=True)
ax.set_ylabel('Count')

Result:

enter image description here

Reason it was not working:

There were two problems:

  • Axes.barh accepts y, width and height. You were passing the first two, but the height was still the default 0.8. The issue is that your y axis is in the range of thousands, so the bars were too tiny to be seen. You can set it to 50 and you will see them:
ax.barh(EleRegion['Elevation'],  EleRegion['Europe'], 50, label = 'Europe')

enter image description here

To avoid playing with height, what you do is use integers (e.g., range(len(EleRegion)) as y and set the yticks to the correct numbers. This way, the default height would have worked.

  • Another issue is that the second bar chart created by using Axes.barh requires the left parameter to really be "stacked", otherwise the second chart is just rendered on top of the first:
f, ax = plt.subplots()
ax.barh(EleRegion['Elevation'], EleRegion['Europe'], 50, label='Europe')
ax.barh(EleRegion['Elevation'], EleRegion['North America'], 50, left=EleRegion['Europe'], label='North America' )
plt.show()

enter image description here

That's why, since your data is already a DataFrame, it's more convenient to use DataFrame.plot.barh to handle all this automatically.

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