【问题标题】:python matplotlib barh reduce gap between barspython matplotlib barh 减少条之间的间隙
【发布时间】:2017-06-22 05:18:32
【问题描述】:

我有一个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()

有没有办法让这些条更靠近?我已经尝试指定图形的大小,但这只会减小整体大小,对间隙大小没有影响...

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

    与条形之间的距离相比,两者都应增加条形的宽度。

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多