python - 当 Pandas 图中的重叠条具有不同的宽度时,它们并不完全居中

标签 python pandas matplotlib

我想知道在使用 pandas 提供的绘图时,是否有办法使条形图中重叠的条居中。

使用 twiny 时,我们得到了对应于不同 plots 的条形重叠,这非常实用。当它们具有相同的宽度时,它们将完美地彼此居中。

然而,当改变其中一组的宽度时,它们将不再居中,这使得情节看起来很乱,如下所示:

Not aligned bars when changing width of the overlay bars

猫 1 和猫 2 的第一个黑条完全位于相应蓝条的中心。然而,下一个黑色条不以橙色条为中心,第三个黑色条不以绿色条为中心。

从这里可以看出,当它们具有相同的宽度时,它们完美居中。那么当它们不具有相同的宽度时,如何以相同的方式居中呢?: enter image description here

这是创建当前未居中图的代码:

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt

txt = '''Category    COLUMN1         COLUMN2     COLUMN3    
A          0.5               3          Cat1   
B          0.3               5          Cat1 
C          0.7               4          Cat1
A          0.4               3          Cat2
B          0.8               5          Cat2
C          0.3               4          Cat2
'''

df = pd.read_table(StringIO(txt), sep="\s+")

order = ['Cat1', 'Cat2']

fig, ax = plt.subplots()
ax2 = ax.twiny()

col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').reindex(order)


col1.plot(kind='bar', ax=ax, edgecolor='Black', lw=1, color='black')

col2 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN2').reindex(order)
col2.plot(kind='bar', ax=ax2, legend=False, alpha=0.7)
ax2.xaxis.set_visible(False)

plt.show() 

编辑

这是一张想要的图片,请原谅“糟糕的图形编辑”。:

enter image description here

最佳答案

首先,您需要让条形图的位置和宽度处于同一比例。所以你可能想要创建一个 twinx 轴而不是 twiny。 (您仍然可以使用 ax.set_ylim(ax2.get_ylim()) 使 y 比例也相等。)

然后,如果主轴上的条和双轴上的条一样多,我们就有了一对一的对应关系,可以根据彩色条的位置计算黑条的位置。

for bbar, cbar in zip(ax.patches, ax2.patches):
    cpos = cbar.get_x()+cbar.get_width()/2.
    bpos = cpos-bbar.get_width()/2.
    bbar.set_x(bpos)

enter image description here

完整的代码,产生上面的情节:

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt

txt = u'''Category    COLUMN1         COLUMN2     COLUMN3    
A          0.5               3          Cat1   
B          0.3               5          Cat1 
C          0.7               4          Cat1
A          0.4               3          Cat2
B          0.8               5          Cat2
C          0.3               4          Cat2
'''

df = pd.read_table(StringIO(txt), sep="\s+")

order = ['Cat1', 'Cat2']

fig, ax = plt.subplots()
ax2 = ax.twinx()

col1 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN1').reindex(order)
col1.plot(kind='bar', ax=ax, edgecolor='Black', lw=1, color='black', width=0.2)
col2 = pd.pivot_table(df,index='COLUMN3',columns='Category',values='COLUMN2').reindex(order)
col2.plot(kind='bar', ax=ax2, legend=False, alpha=0.7)
ax2.xaxis.set_visible(False)
ax.set_ylim(ax2.get_ylim())

for bbar, cbar in zip(ax.patches, ax2.patches):
    cpos = cbar.get_x()+cbar.get_width()/2.
    bpos = cpos-bbar.get_width()/2.
    bbar.set_x(bpos)

plt.show() 

关于python - 当 Pandas 图中的重叠条具有不同的宽度时,它们并不完全居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214474/

相关文章:

python - 使用内存引用重载 [] python 运算符和链接方法

python - 按 NaN 计数的降序对数据帧的行进行排序

python - 在 DataFrame 中查找第一次出现的索引

python - 如何按 pandas.value_counts 对结果进行排序

python - wxpython-plotting 中的 Matplotlib 中止

python - 如何更改 matplotlib 图中刻度的字体大小?

python - 此正则表达式的对抗性输入

python - IPython Notebook 输出单元格正在截断我的列表的内容

python - 在python中将文件保存到名称以0开头的目录

pandas - 如何获取 pandas 中的行索引?