我想從 python 計數器繪制條形圖。
toplist = countersum.most_common(len(countersum))
topkeylist = list()
topvallist = list()
for i in range(len(toplist)):
topkeylist.append(toplist[i][0])
topvallist.append(toplist[i][1])
for i in range(len(toplist)):
print(topkeylist[i],":" ,topvallist[i])
keylist = list()
for key in topkeylist:
keylist.append(str(key))
plt.barh(keylist[:100], topvallist[:100], height = 0.3, align = 'center')
plt.xlabel('Occurence inside the dataset')
plt.ylabel('N-gram sample')
plt.show()
它給了我以下情節:

我想設定條之間的空間,以便它們甚至可以觸摸并將文本與條對齊 - 因為文本大小將與條的寬度相同,以便更好地閱讀。
uj5u.com熱心網友回復:
您可以在此處使用 width 引數調整條形的寬度:
plt.barh(keylist[:100], topvallist[:100], height = 0.3, align = 'center', width=5).
此外,嘗試減小 y 軸上的字體大小,以便刻度線更清晰:
plt.yticks(fontsize=10).
如果這不起作用,您可以使用增加圖形的大小
fig = plt.figure(figsize=(18,18))
uj5u.com熱心網友回復:
您可以在 a 中設定您想要的內容dict并使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335140.html
標籤:Python matplotlib 条形图 柜台 图表
