【问题标题】:How to decrease the spaces between two bars in matptolib?如何减少matplotlib中两个条之间的空间?
【发布时间】:2019-04-29 14:25:54
【问题描述】:

我正在尝试使用 matplotlib 获取条形图。图中只有 2 个条形,但它们之间的空间非常大。我试图减少和增加宽度,但它只是改变了条形的宽度。

def evaluate_bar_graph(coherences, indices):

        assert len(coherences) == len(indices)
        n = len(coherences)
        x = np.arange(n)
        plt.bar(x, coherences, width=0.1, tick_label=indices, align='center')
        plt.xlabel('Models')
        plt.ylabel('Coherence Value')
        plt.show()

【问题讨论】:

  • @Goyo 没有其他解决方案吗?
  • @Sheldore 我不要求工作答案。我在问有什么办法可以减少空间
  • “我试图减少和增加宽度,但它只是改变了条形宽度”是什么意思,这不正是您想要做的吗?让x=[0,1]coherences=[1,2],一旦你到达width=1,条就会“接触”;如果x=[0,2],它们之间将有“1”!
  • width 只是改变了条形宽度。如您所见,我想减少条形之间的空间。不要增加条形宽度

标签: python python-3.x matplotlib


【解决方案1】:

您可以通过使用 y_pos 选择条形的位置然后重命名 x 轴来实现。 喜欢:

# space= 1 or 2

y_pos = [0, 1] # or y_pos = [0, 1]

# Create bars
plt.bar(y_pos, coherences)

# Create names on the x-axis
plt.xticks(y_pos, x)

# Show graphic
plt.show()

【讨论】: