简体   繁体   English

python matplotlib barh减少了条之间的差距

[英]python matplotlib barh reduce gap between bars

I have a barh bar chart with two bars only in the figure, but when plotting them, they are very far apart: 我有一个barh有两个酒吧条形图只在图中,但是打印时它们,它们相距甚远很:

import numpy as np
import matplotlib.pyplot as plt

labels = ('Out', 'In')
bar_values = [5, 10]
num_items = len(labels)
width = 0.25
ind = np.arange(num_items)
bar_width = 0.1

fig = plt.figure()

ax = fig.add_subplot(111)

barlist = ax.barh(ind,
                bar_values,
                bar_width,
                align='edge',
                color='green')

barlist[0].set_color('mediumseagreen')
barlist[1].set_color('orangered')

ax.set_yticks(ind)
ax.set_yticklabels(labels)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Total')

plt.show()

Is there a way of putting the bars closer together? 有没有办法把酒吧放在一起? I have already tried to specify the size of the figure, but that only reduces the overall size and has no impact on the gap size... 我已经尝试指定图形的大小,但这只会减小整体尺寸,对间隙尺寸没有影响......

You could either just increase the width of the bars by setting bar_width = 0.6 or a similar value, or you could reduce the y range of the figure, for example: 您可以通过设置bar_width = 0.6或类似值来增加条形的宽度,或者可以减小图形的y范围,例如:

barlist = ax.barh([0.1, 0.3],
                bar_values,
                bar_width,
                align='edge',
                color='green')
ax.set_yticks([0.1, 0.3])
ax.set_yticklabels(labels)

Both should increase the width of the bars compared to the distance between the bars. 两者都应该增加杆的宽度与杆之间的距离。 减少y范围

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

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