【matplotlib】消除條形圖bar的間隙

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

在使用matplotlib繪製條形圖時,由於width默認爲0.8,條形圖中會產生很多間隙,使用bar函數時,添加width = 1即可消除間隔

消除間隔前

消除間隔後

import matplotlib.pyplot as plt
import random

dic={}
for i in range(1000):
    dic[int(i)]=random.randint(30,100)
plt.bar(dic.keys(), dic.values(), width=1, alpha=0.5, color='b')
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章