简体   繁体   English

两个不同比例并排的条形图

[英]Two Bar Plots Side by Side with Different Scales

I am using the following matplotlib code to create two bar plots side by side.我正在使用以下 matplotlib 代码并排创建两个条形图。 But they are in different scales.但它们的规模不同。 I would like to remove the unwanted white region.我想删除不需要的白色区域。

action_number = [5, 6, 6, 6, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                5, 6, 6, 6, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
action_list = ['DrawSword', 'Kiss', 'RideBike', 'Dribble', 'Hug', 'Dive', 
               'Cartwheel', 'Drink', 'Climb', 'Eat', 'Walk', 'FallFloor', 
               'Stand', 'Sword', 'Pick', 'SwingBaseb', 'Handstand', 'ShootBow',
               'DrawSword', 'Kiss', 'RideBike', 'Dribble', 'Hug', 'Dive', 
               'Cartwheel', 'Drink', 'Climb', 'Eat', 'Walk', 'FallFloor', 
               'Stand', 'Sword', 'Pick', 'SwingBaseb', 'Handstand', 'ShootBow']

def bar_plot(action_list, action_number, ax):
    y_pos = np.arange(len(action_list))

    ax.barh(y_pos, action_number, height=0.75, align='center', color='b')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(action_list)

    ax.invert_yaxis()

    ax.tick_params(labelsize=12, which='both', axis='both')

    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')

    plt.autoscale(tight=True)
    plt.tight_layout()

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
fig.subplots_adjust(wspace=0, hspace=0)
bar_plot(action_list, action_number, ax1)
bar_plot(action_list, action_number, ax2)
plt.savefig('action_attribute_sidebyside.pdf',bbox_inches="tight", pad_inches=0)

Here is the figure generated by the code.下面是代码生成的图。 I highlight the unwanted white region in red boxes.我在红色框中突出显示不需要的白色区域。 矩阵

remove plt.autoscale and put plt.tight_layout out of bar_plot :删除plt.autoscale并将plt.tight_layout放在plt.tight_layout bar_plot

def bar_plot(action_list, action_number, ax):
    y_pos = np.arange(len(action_list))

    ax.barh(y_pos, action_number, height=0.75, align='center', color='b')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(action_list)

    ax.invert_yaxis()

    ax.tick_params(labelsize=12, which='both', axis='both')

    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')

    # set the limits manually
    ax.set_ylim(-.5, len(action_list)-0.5)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
fig.subplots_adjust(wspace=0, hspace=0)
bar_plot(action_list, action_number, ax1)
bar_plot(action_list, action_number, ax2)

plt.tight_layout()

Output:输出:

在此处输入图片说明

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

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