【问题标题】:Not able to produce gaps between bars in matplotlib bar chart in Python无法在 Python 中的 matplotlib 条形图中的条形之间产生间隙
【发布时间】:2016-12-05 04:30:57
【问题描述】:

我无法在 Python 中的 matplotlib 生成的条形图中每 3 个条形之间产生间隙。目前,所有条形都聚集在一起,我的目标是在图中每 3 个条形之间有 1 个条形宽度。我的代码如下:

import numpy as np
import matplotlib.pyplot as plt
N = 3

ind = np.arange(N)
width = 0.35
fig, ax = plt.subplots()

accu = [0.72, 0.99, 0.83]
rects1 = ax.bar(ind, accu, width, color='b')
prec = [0.99, 0.99, 0.81]
rects2 = ax.bar(ind+width, prec, width, color='y')
rec = [0.61, 0.99, 0.99]
rects3 = ax.bar(ind + 2 * width, rec, width, color='r')

ax.set_ylabel('Scores')
ax.set_title('Scores for each algorithm')
ax.set_xticks(ind + 1.5*width)
ax.set_xticklabels(('SVM', 'DecisionTree', 'NaiveBayes'))
ax.legend((rects1[0], rects2[0], rects3[0]), ('Accuracy', 'Precision', 'Recall'))

plt.show()

目前的地块如下所示

【问题讨论】:

  • 让宽度变窄?
  • 啊,正是我需要的,谢谢 :)
  • 您应该发布自己问题的答案并接受(系统允许时)。

标签: python matplotlib bar-chart


【解决方案1】:

正如@tacaswell 在 cmets 中指出的那样,条形之间的间隙是通过减小条形宽度来实现的。宽度=0.25,我们得到下图

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2022-01-17
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多