Python Matplotlib Barh 减少条形图之间的间距

4

我有一个水平条形图 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()

有没有办法使图表中的条形之间更加接近?我已经尝试指定图表的大小,但这只会减小整个图表的大小,对条形之间的间隔大小没有影响...

1个回答

3

您可以通过设置 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)

两者都应该增加条形图的宽度,使之比条形之间的距离更宽。

收缩 y 轴范围

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接