简体   繁体   English

使用pandas定位多个堆积条形图

[英]Positioning of multiple stacked bar plot with pandas

I am trying to make a multiple stacked bar plot with pandas. 我正在尝试用熊猫制作一个多叠的条形图。

Here is a sample code: 这是一个示例代码:

import pandas as pd

df = pd.DataFrame({'a':[10, 20], 'b': [15, 25], 'c': [35, 40], 'd':[45, 50]}, index=['john', 'bob'])

ax = df[['a', 'c']].plot.bar(position=0, width=0.1, stacked=True)
df[['b', 'd']].plot.bar(position=1, width=0.1, stacked=True, ax=ax)

Which, in a notebook, give the following output: 在笔记本中,它提供以下输出: 堆积条形图

The problem I'm facing is that, for each group, the bars are not in the order I want them to be. 我面临的问题是,对于每个组,条形不符合我希望它们的顺序。 The doc says that the "position" argument for a bar plot specifies the relative position of the bar, with 0 being left and 1 being right. 文档说条形图的“位置”参数指定条形图的相对位置,其中0表示左侧,1表示右侧。 But it seems to do the complete opposite. 但似乎完全相反。 What am I misunderstanding? 我有什么误会?

The position parameter in pd.Df.plot controls the alignment of your bar plot layouts. pd.Df.plot的position参数控制条形图布局的对齐方式。 It resembles similarity with the align parameter of a matplotlib bar plot. 它类似于matplotlib条形图的align参数的相似性。

Illustrations: 插图:

1. when position=0.5 : [Default : center alignment] 1.当position=0.5 :[默认:居中对齐]

df[['a', 'c']].plot.bar(position=0.5, width=0.1, stacked=True, ax=ax)
df[['b', 'd']].plot.bar(position=0.5, width=0.1, stacked=True, ax=ax)

The bars are coinciding with the X-axis labels as shown: 条形图与X轴标签重合,如下所示: 在此输入图像描述 2. when position=0 :[left/bottom edge alignment] 2.当position=0 :[left / bottom edge alignment]

df[['a', 'c']].plot.bar(position=0, width=0.1, stacked=True, ax=ax)
df[['b', 'd']].plot.bar(position=0, width=0.1, stacked=True, ax=ax)

The left most portion of the bars are coinciding with the X-axis labels as shown: 条的最左侧部分与X轴标签重合,如图所示: 在此输入图像描述

Now, you can clearly distinguish the difference between the above 2 figures. 现在,您可以清楚地区分上述2个数字之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM