網站首頁 編程語言 正文
項目場景:
Python項目需要畫兩組數(shù)據(jù)的雙柱狀圖,以下以一周七天兩位小朋友吃糖顆數(shù)為例進行演示,用matplotlib庫實現(xiàn)
代碼:
import matplotlib import matplotlib.pyplot as plt import numpy as np def drawHistogram(): matplotlib.rc("font", family='MicroSoft YaHei') list1 = np.array([5, 2, 1, 0, 8, 0, 6]) # 柱狀圖第一組數(shù)據(jù) list2 = np.array([9, 5, 1, 2, 9, 2, 0]) # 柱狀圖第二組數(shù)據(jù) length = len(list1) x = np.arange(length) # 橫坐標范圍 listDate = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] plt.figure() total_width, n = 0.8, 2 # 柱狀圖總寬度,有幾組數(shù)據(jù) width = total_width / n # 單個柱狀圖的寬度 x1 = x - width / 2 # 第一組數(shù)據(jù)柱狀圖橫坐標起始位置 x2 = x1 + width # 第二組數(shù)據(jù)柱狀圖橫坐標起始位置 plt.title("一周每天吃悠哈軟糖顆數(shù)柱狀圖") # 柱狀圖標題 # plt.xlabel("星期") # 橫坐標label 此處可以不添加 plt.ylabel("吃悠哈軟糖顆數(shù)(個)") # 縱坐標label plt.bar(x1, list1, width=width, label="小s吃糖數(shù)") plt.bar(x2, list2, width=width, label="小y吃糖數(shù)") plt.xticks(x, listDate) # 用星期幾替換橫坐標x的值 plt.legend() # 給出圖例 plt.show() if __name__ == '__main__': drawHistogram()
效果圖:
擴展功能及代碼:
擴展功能一
如果橫坐標標簽比較長或是文字比較多,以一定角度傾斜展示,上文中代碼這一行:
plt.xticks(x, listDate)
可以改為:
plt.xticks(x, listDate, rotation=30) # rotation為標簽旋轉角度
橫坐標標簽旋轉30°效果如下:
橫坐標標簽旋轉90°效果如下:
擴展功能二
如果希望具體的數(shù)據(jù)值展示在柱狀圖中,可以在代碼 plt.legend()
前加入如下代碼:
for a, b in zip(x1, list1): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7) for a, b in zip(x2, list2): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7)
加了具體數(shù)值的柱狀圖效果如下:
補充:Python畫圖實現(xiàn)同一結點多個柱狀圖
import numpy as np x = [1,2] #橫坐標 y = [3,4] #第一個縱坐標 y1 = [5,6] #第二個縱坐標 x = np.arange(len(x)) #首先用第一個的長度作為橫坐標 width = 0.05 #設置柱與柱之間的寬度 fig,ax = plt.subplots() ax.bar(x,y,width,alpha = 0.9) ax.bar(x+width,y1,width,alpha = 0.9,color= 'red') ax.set_xticks(x +width/2)#將坐標設置在指定位置 ax.set_xticklabels(x)#將橫坐標替換成 plt.show()
后續(xù)有時間再繼續(xù)補充擴展功能哦~
總結
原文鏈接:https://blog.csdn.net/qq_39691492/article/details/119422424
相關推薦
- 2023-05-12 Python?pandas?的索引方式?data.loc[],data[][]示例詳解_python
- 2022-04-28 Python字符串的創(chuàng)建和駐留機制詳解_python
- 2023-11-16 當pytorch找不到 CUDA 時,如何修復錯誤:版本 libcublasLt.so.11 未在帶
- 2023-04-02 vscode?ssh遠程連接服務器的思考淺析_相關技巧
- 2022-08-19 Spring Cloud Config配置服務
- 2023-02-27 nginx編譯安裝及常用參數(shù)詳解_nginx
- 2022-09-30 docker部署golang?http服務時端口無法訪問的問題解決_docker
- 2022-06-21 Flutter實現(xiàn)單選,復選和開關組件的示例代碼_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支