網(wǎng)站首頁 編程語言 正文
前言
以下分享折線圖小案例,matplotlib還可以進行多種圖形的繪制,可以進入官網(wǎng)?https://matplotlib.org/gallery/index.html,點擊examples,如需學習,選擇要學習的圖進入,里面包含有代碼
python之matplotlib使用系統(tǒng)字體
1.導(dǎo)包from matplotlib.font_manager import FontProperties2.調(diào)用本機字體庫設(shè)置字體my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")
其中,msyh.ttc是自己電腦中的字體,如何找到呢?
在路徑C:\Windows\Fonts的文件夾中,有如下字體,如下圖:
繪圖的時候,直接調(diào)用就好
??plt.xticks(list(x)[::3],_xtick_labels[::3],rotatinotallow=45,fnotallow=my_font)?
?
實例1:溫度變化統(tǒng)計
#如果列表a便是10點到12點的每一分鐘的氣溫,繪制折線圖 # a=[random.randint(20,35)for i in range(120)] #解決中文不顯示問題 #fc-list -->查看支持的字體 #fc-list :lang=zh -->查看支持的中文(冒號前有空格) from matplotlib import pyplot as plt import random import matplotlib from matplotlib import font_manager #1.windows\linux設(shè)置字體 #font = {'family' : 'MicroSoft YaHei', # 'weight' : 'bold', # 'size' : 'larger'} #matplotlib.rc("font",**font) #查看源碼ctrl+b #2.另一種設(shè)置字體方式 my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc") #定義x、y軸 x = range(0,120) y = [random.randint(20,35) for i in range(120)] #修改大小尺寸 plt.figure(figsize=(20,8),dpi=80) #繪制 plt.plot(x,y) #調(diào)整x軸的刻度 _xtick_labels = ["10點{}分".format(i) for i in range(60)] _xtick_labels += ["11點{}分".format(i) for i in range(60)] #取適當步長,將數(shù)字與x軸字符串對應(yīng),使得數(shù)據(jù)長度保持一致 plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font) #將x軸字符串旋轉(zhuǎn)45度 #添加描述信息 plt.xlabel("時間",fontproperties=my_font) plt.ylabel("溫度 單位(°c)",fontproperties=my_font) plt.title("10點到12點每分鐘的氣溫變化情況",fontproperties=my_font) #顯示圖示 plt.show()
實例2:交友數(shù)量折線圖
#你與朋友從11到30歲交的朋友數(shù),并比較 from matplotlib import pyplot as plt from matplotlib import font_manager #設(shè)置字體 my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc") #定義坐標軸 x = range(11,31) y_1 = [2,3,3,4,6,5,6,5,8,5,4,6,4,4,4,4,4,3,3,3] y_2 = [1,4,5,5,6,4,5,5,4,7,6,5,3,2,2,6,1,2,6,4] #設(shè)置圖形大小 plt.figure(figsize=(20,8),dpi=80) #繪制 plt.plot(x,y_1) plt.plot(x,y_2) #繪制x\y軸刻度,添加描述信息 _xtick_labels = ["{}歲".format(i) for i in x] plt.xticks(x,_xtick_labels,fontproperties=my_font) plt.yticks(range(0,10)) plt.xlabel("年齡",fontproperties=my_font) plt.ylabel("每年交到的新朋友數(shù)",fontproperties=my_font) plt.title("與朋友每年新交到朋友數(shù)量對比圖",fontproperties=my_font) #繪制網(wǎng)格,并設(shè)置透明度 plt.grid(alpha=0.3) #展示 plt.show()
1.這個案例中涉及到一表多圖,其實很簡單,與單圖設(shè)計一樣,只要再添加一組y軸坐標。
這里x軸是共有的,不需要另行設(shè)置。#定義坐標軸?
x = range(11,31)
y_1 = [2,3,3,4,6,5,6,5,8,5,4,6,4,4,4,4,4,3,3,3]
y_2 = [1,4,5,5,6,4,5,5,4,7,6,5,3,2,2,6,1,2,6,4]
#繪制
plt.plot(x,y_1)
plt.plot(x,y_2)
2.繪制網(wǎng)格及設(shè)置透明度
plt.grid(alpha=0.3)
3.但是當你給別人展示時,并沒有源碼,別人很難分清哪個曲線是你的,哪個是你朋友的,這時就需要我們添加圖例,并且要注意的是:
通常我們設(shè)置中文字體是對應(yīng)方法后添加fontproperties=my_font
,但是在添加圖例中用到的是prop=my_font
如圖所示:
4.更改圖例位置
由于初學,很多方法我們還不是很清楚,所以我們要學會查看源碼(選中方法名+ctrl+b)
再使用一次,進入后會找到有關(guān)參數(shù)loc(location)的描述,我們設(shè)置loc=“upper left”,結(jié)果如圖所示
5.設(shè)置曲線顏色,線條樣式
#繪制,添加顏色 plt.plot(x,y_1,label="自己",color="y") plt.plot(x,y_2,label="朋友",color="cyan")
#繪制,添加線條類型 plt.plot(x,y_1,label="自己",color="purple",linestyle='-.') plt.plot(x,y_2,label="朋友",color="cyan",linestyle='--')
原文鏈接:https://blog.51cto.com/u_15749390/5577213
相關(guān)推薦
- 2022-11-20 React?跨端動態(tài)化核心技術(shù)實例分析_React
- 2024-03-06 SpringAOP基于注解方式實現(xiàn)和細節(jié)
- 2022-07-03 Go?的入口函數(shù)和包初始化的使用_Golang
- 2024-03-09 【Redis】什么是緩存雪崩,如何預(yù)防緩存雪崩?
- 2022-08-01 Flask框架之數(shù)據(jù)交互的實現(xiàn)_python
- 2022-04-17 Mac使用pandoc 將docx文件轉(zhuǎn)換成html文件 快速實現(xiàn)協(xié)議文件的轉(zhuǎn)換
- 2022-04-03 帶你理解C語言中的漢諾塔公式_C 語言
- 2022-12-21 Python?threading中l(wèi)ock的使用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支