繁体   English   中英

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

[英]Not able to produce gaps between bars in matplotlib bar chart in Python

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

当前的站立图如下所示

条形图,每3条之间没有间隙

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

校正图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM