在Python中的matplotlib条形图中,无法在条形图之间产生间隙

时间:2016-12-05 04:30:58

标签: python matplotlib bar-chart

我无法在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()

目前的常设情况如下:

Bar chart with no gaps between every 3 bars

1 个答案:

答案 0 :(得分:0)

正如@tacaswell的评论所指出的,通过减小条宽来实现条之间的间隙。宽度= 0.25时,我们得到下图

corrected graph