【问题标题】:Overlapping bars in pandas plot are not perfectly centered over each other when they have different width当它们具有不同的宽度时,熊猫图中的重叠条不会完全居中
【发布时间】:2018-08-19 05:57:05
【问题描述】:

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

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

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

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

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

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

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() 

编辑

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

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    首先,您需要将条形位置和宽度保持在相同的比例上。所以你可能想要创建一个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)
    

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

    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() 
    

    【讨论】:

    • 这看起来更好,但这不是我想要的。很抱歉没有在最初的帖子中更好地解释它。我现在添加了一张图片来澄清,使用图片更容易解释和理解。不过谢谢!
    • 是的图片总是有用。
    猜你喜欢
    • 2019-08-04
    • 2017-11-07
    • 1970-01-01
    • 2012-12-06
    • 2018-11-13
    • 2015-11-29
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多