網站首頁 編程語言 正文
官網: https://matplotlib.org
一、版本
# 01 matplotlib安裝情況 import matplotlib matplotlib.__version__
二、圖表主題設置
請點擊:圖表主題設置
三、一次函數
import numpy as np from matplotlib import pyplot as plt # 如何使用中文標題 plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 x = np.arange(1,11) y = 2 * x + 5 # 圖片顯示的是這個公式 plt.title("Matplotlib展示") plt.xlabel("x軸") plt.ylabel("y軸") plt.plot(x,y) plt.show()
四、多個一次函數
創建一個關于電影票房的圖表:
films=['穿過寒冬擁抱你','反貪風暴5:最終章','李茂扮太子','誤殺2','以年為單位的戀愛','黑客帝國:矩陣重啟','雄獅少年','魔法滿屋','汪汪隊立大功大電影','愛情神話'] regions=['中國','英國','澳大利亞','美國','美國','中國','英國','澳大利亞','美國','美國'] bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979'] persons=['31','23','56','17','9','31','23','56','17','9'] prices=['51','43','56','57','49','51','43','56','57','49'] showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05'] ftypes=['劇情','動作','喜劇','劇情','劇情','愛情','動作','動畫','動畫','動畫'] points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8'] filmdescript={ 'ftypes':ftypes, 'bos':bos, 'prices':prices, 'persons':persons, 'regions':regions, 'showdate':showdate, 'points':points }
import numpy as np import pandas as pd cnbo2021top5=pd.DataFrame(filmdescript,index=films) cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int) cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int) cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64') cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)
關于cnboo1.xlsx,我放在我的碼云里,需要的朋友自行下載:cnboo1.xlsx
# 讀取并初步整理數據集 import pandas as pd cnbodf=pd.read_excel('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
def mkpoints(x,y): # 編寫points評分 return len(str(x))*(y/25)-3 cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfsort.to_excel("cnbodfsort.xlsx",index=False) # 創建一個Excel文件
from matplotlib import pyplot as plt plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("票房2021TOP5") plt.xlabel("x軸") plt.ylabel("y軸") x=cnbo2021top5.persons.sort_values() y=cnbo2021top5.prices.sort_values() plt.plot(x,y,marker=".",markersize=20,color='red',linewidth=4,markeredgecolor='blue') plt.show()
# 折線圖進階 from matplotlib import pyplot as plt plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國票房2021TOP5") plt.plot(bo,prices,label='票房與票價') plt.plot(bo,persons,label='票房與人次') plt.plot(bo,points,label='票房與評價') plt.legend() # 顯示標簽 plt.xlabel('票房') # 橫坐標軸 plt.ylabel('行情') # 縱坐標軸 plt.show()
更改一下版式
# 折線圖進階 from matplotlib import pyplot as plt plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國票房2021TOP5") plt.plot(bo,prices,'r^--',label='票房與票價') plt.plot(bo,persons,'g*-',label='票房與人次') plt.plot(bo,points,color='blue',marker='o',markersize=10,label='票房與評價') plt.legend() # 顯示標簽 plt.xlabel('票房') # 橫坐標軸標題 plt.ylabel('行情') # 縱坐標軸標題 plt.show()
五、填充折線圖
填充折線圖:當確定一條數據線上面的一點的時候,能夠將該點的上下兩部分分別使用不同的顏色填充。
dev_x=[25,26,27,28,29,30] # 開發者的年齡 dev_y=[7567,8789,8900,11560,16789,25231] #收入情況 py_dev_y=[5567,6789,9098,15560,20789,23231] # python開發者 js_dev_y=[6567,7789,8098,12356,14789,20231] # java開發者 devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y]) devsalaryT=pd.DataFrame(devsalary.values.T,columns=["Age","Dev","Python","Java"])
# 繪制帶陰影的折線圖 from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(7,4)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("開發人員薪資情況") baseline=10000 plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資") plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有label是不會顯示legend的數據標簽的 plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow') plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red') plt.grid() plt.legend() plt.show()
# 繪制帶陰影的折線圖 from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(7,4)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("開發人員薪資情況") baseline=10000 plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資") plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有label是不會顯示legend的數據標簽的 plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow',alpha=0.3) plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red',alpha=0.3) # alpha=0.3調整透明度 plt.grid() plt.legend() plt.show()
# 繪制帶陰影的折線圖 from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(7,4)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("開發人員薪資情況") baseline=10000 plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資") plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有label是不會顯示legend的數據標簽的 plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='pink',alpha=0.7,label="高于10000元") plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='purple',alpha=0.7,label="低于或等于10000元") # alpha=0.3調整透明度 plt.grid() plt.legend() plt.show()
interpolate=True:將交叉的位置進行填充
# 繪制帶陰影的折線圖 from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(7,4)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("開發人員薪資情況") plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資") plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有label是不會顯示legend的數據標簽的 plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]>baseline),interpolate=True,color='green',alpha=0.7,label="高于總體") plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]<=baseline),interpolate=True,color='tomato',alpha=0.7,label="低于或等于總體") # alpha=0.3調整透明度 plt.grid() plt.legend() plt.show()
原文鏈接:https://blog.csdn.net/wxfighting/article/details/123299844
相關推薦
- 2022-04-22 Golang執行流程詳解,兩種執行流程方式有什么不同
- 2023-02-25 C++中vector迭代器失效與深淺拷貝問題詳析_C 語言
- 2023-05-29 Postgresql數據庫角色創建登錄詳解_PostgreSQL
- 2022-10-01 python3中_from...import...與import?...之間的區別詳解(包/模塊)_
- 2023-01-02 C語言中讀寫交替時出現的問題分析_C 語言
- 2023-03-22 PyTorch中torch.tensor()和torch.to_tensor()的區別_python
- 2022-02-10 Linux高并發踩過的坑及性能優化介紹_Linux
- 2023-01-20 python的程序分支結構用法及說明_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支