不能在堆叠的条形图中绘制所有数据吗?
我尝试绘制水平堆叠的条形图,其高度为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' )
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
高程设置为索引,并使用
dataframe.plot.barh
:结果:
原因不起作用:
有两个问题:
axes.barh
接受,<代码>宽度和高度
。您正在传递前两个,但是高度仍然是默认0.8
。问题在于您的y
轴在数千个范围内,因此条太小了,无法看到。您可以将其设置为50,您会看到它们: yticks 设置为正确的数字
y
,将是使用
axes.barh
创建的第二个条形图要求左
参数真正被“堆叠”,否则第二个图表仅在第一个图表的顶部呈现:这就是为什么,由于您的数据已经是
dataframe
,因此使用dataframe.plot.barh
更方便地自动处理所有这些。You can set
Elevation
as index and useDataFrame.plot.barh
:Result:
Reason it was not working:
There were two problems:
Axes.barh
acceptsy
,width
andheight
. You were passing the first two, but the height was still the default0.8
. The issue is that youry
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:To avoid playing with
height
, what you do is use integers (e.g.,range(len(EleRegion))
asy
and set theyticks
to the correct numbers. This way, the defaultheight
would have worked.Axes.barh
requires theleft
parameter to really be "stacked", otherwise the second chart is just rendered on top of the first:That's why, since your data is already a
DataFrame
, it's more convenient to useDataFrame.plot.barh
to handle all this automatically.