網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
項(xiàng)目場(chǎng)景:
Python項(xiàng)目需要畫兩組數(shù)據(jù)的雙柱狀圖,以下以一周七天兩位小朋友吃糖顆數(shù)為例進(jìn)行演示,用matplotlib庫(kù)實(shí)現(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) # 橫坐標(biāo)范圍 listDate = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] plt.figure() total_width, n = 0.8, 2 # 柱狀圖總寬度,有幾組數(shù)據(jù) width = total_width / n # 單個(gè)柱狀圖的寬度 x1 = x - width / 2 # 第一組數(shù)據(jù)柱狀圖橫坐標(biāo)起始位置 x2 = x1 + width # 第二組數(shù)據(jù)柱狀圖橫坐標(biāo)起始位置 plt.title("一周每天吃悠哈軟糖顆數(shù)柱狀圖") # 柱狀圖標(biāo)題 # plt.xlabel("星期") # 橫坐標(biāo)label 此處可以不添加 plt.ylabel("吃悠哈軟糖顆數(shù)(個(gè))") # 縱坐標(biāo)label plt.bar(x1, list1, width=width, label="小s吃糖數(shù)") plt.bar(x2, list2, width=width, label="小y吃糖數(shù)") plt.xticks(x, listDate) # 用星期幾替換橫坐標(biāo)x的值 plt.legend() # 給出圖例 plt.show() if __name__ == '__main__': drawHistogram()
效果圖:
擴(kuò)展功能及代碼:
擴(kuò)展功能一
如果橫坐標(biāo)標(biāo)簽比較長(zhǎng)或是文字比較多,以一定角度傾斜展示,上文中代碼這一行:
plt.xticks(x, listDate)
可以改為:
plt.xticks(x, listDate, rotation=30) # rotation為標(biāo)簽旋轉(zhuǎn)角度
橫坐標(biāo)標(biāo)簽旋轉(zhuǎn)30°效果如下:
橫坐標(biāo)標(biāo)簽旋轉(zhuǎn)90°效果如下:
擴(kuò)展功能二
如果希望具體的數(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ù)值的柱狀圖效果如下:
補(bǔ)充:Python畫圖實(shí)現(xiàn)同一結(jié)點(diǎn)多個(gè)柱狀圖
import numpy as np x = [1,2] #橫坐標(biāo) y = [3,4] #第一個(gè)縱坐標(biāo) y1 = [5,6] #第二個(gè)縱坐標(biāo) x = np.arange(len(x)) #首先用第一個(gè)的長(zhǎng)度作為橫坐標(biāo) width = 0.05 #設(shè)置柱與柱之間的寬度 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)#將坐標(biāo)設(shè)置在指定位置 ax.set_xticklabels(x)#將橫坐標(biāo)替換成 plt.show()
后續(xù)有時(shí)間再繼續(xù)補(bǔ)充擴(kuò)展功能哦~
總結(jié)
原文鏈接:https://blog.csdn.net/qq_39691492/article/details/119422424
相關(guān)推薦
- 2023-06-17 tensorflow1.x和tensorflow2.x中的tensor轉(zhuǎn)換為字符串的實(shí)現(xiàn)_pytho
- 2023-07-04 JUC 之CountDownLatch工具類
- 2022-11-26 python?布爾注入原理及滲透過(guò)程示例_python
- 2023-11-16 nvidia jetson設(shè)備(Jetson Nano, TX1/TX2,Xavier NX/AGX
- 2022-10-14 面試官:int(1) 和 int(10) 有什么區(qū)別?
- 2024-02-16 springmvc中的數(shù)據(jù)提交方式
- 2022-09-09 C#正則表達(dá)式與HashTable詳解_C#教程
- 2022-06-15 Python遞歸生成全排列序列實(shí)操_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支