簡體   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