用Python 繪制 柱狀圖,使用的是bar()函數。
一個簡單的例子:
# 創建一個點數為 8 x 6 的窗口, 並設置分辨率為 80像素/每英寸 plt.figure(figsize=(10, 10), dpi=80) # 再創建一個規格為 1 x 1 的子圖 # plt.subplot(1, 1, 1) # 柱子總數 N = 10 # 包含每個柱子對應值的序列 values = (56796,42996,24872,13849,8609,5331,1971,554,169,26) # 包含每個柱子下標的序列 index = np.arange(N) # 柱子的寬度 width = 0.45 # 繪制柱狀圖, 每根柱子的顏色為紫羅蘭色 p2 = plt.bar(index, values, width, label="num", color="#87CEFA") # 設置橫軸標簽 plt.xlabel('clusters') # 設置縱軸標簽 plt.ylabel('number of reviews') # 添加標題 plt.title('Cluster Distribution') # 添加縱橫軸的刻度 plt.xticks(index, ('mentioned1cluster', 'mentioned2cluster', 'mentioned3cluster', 'mentioned4cluster', 'mentioned5cluster', 'mentioned6cluster', 'mentioned7cluster', 'mentioned8cluster', 'mentioned9cluster', 'mentioned10cluster')) # plt.yticks(np.arange(0, 10000, 10)) # 添加圖例 plt.legend(loc="upper right") plt.show()
結果:
【Reference】
1、https://blog.csdn.net/qq_41011336/article/details/83016709 (含參數解釋)