Python實現柱狀圖【數字精准展示,使用不同顏色】


一.簡介

  主要使用matplotlib基於python的可視化組件實現。

二.代碼實現 

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Mon Jul 13 09:55:30 2019
 4 @author: zhen
 5 """
 6 import numpy as np
 7 import matplotlib.pyplot as plt
 8 
 9 plt.rcParams['font.sans-serif']=['SimHei']   # 用黑體顯示中文
10 plt.rcParams['axes.unicode_minus']=False     # 正常顯示負號
11 
12 x = np.array(["稽查","流程","核實","問題","現場","質量","管理","用戶","系統","整改"])  # x值取默認值
13 y = np.array([136, 216, 186, 192, 132, 122, 111, 110, 161, 86])
14 
15 sortIndex = np.argsort(-y) # 倒序,返回排序后各數據的原始下標
16 
17 x_sort = x[sortIndex] # 重新進行排序,與y保持初始順序一致
18 y_sort = y[sortIndex] # 重新進行排序,倒序
19 
20 #定義函數來顯示柱狀上的數值
21 def autolabel(rects):
22     for rect in rects:
23         height = rect.get_height()
24         plt.text(rect.get_x()+rect.get_width()/2.-0.25, 1.01*height, '%s' % int(height))
25 
26 plt.xticks(np.arange(len(x_sort)), x_sort)
27 a = plt.bar(np.arange(len(x_sort)),y_sort,color=['r','g','b', 'c', 'm', 'y'])
28 autolabel(a)
29 
30 plt.title('案例庫熱詞top10') 
31 plt.ylabel('詞頻', fontsize=12) 
32 plt.xlabel('詞語', fontsize=12) 
33 plt.show()

三.結果

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM