python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充
Matplotlib配置了配色方案和默認(rèn)設(shè)置,主要用來(lái)準(zhǔn)備用于發(fā)布的圖片。有兩種方式可以設(shè)置參數(shù),即全局參數(shù)定制和rc設(shè)置方法。
查看matplotlib的rc參數(shù):
import matplotlib as plt print(plt.ra_params)
1.全局參數(shù)定制
Matplotlib的全局參數(shù)可以通過(guò)編輯配置文件設(shè)置
import matplotlib as plt print(plt.matplotlib_fname) #顯示當(dāng)前用戶的配置文件目錄
查找到當(dāng)前用戶的配置文件目錄,然后用編輯器打開,修改matplotlib文件,即可修改配置參數(shù)。
2.rc參數(shù)設(shè)置
使用Python編程修改rc參數(shù),rc參數(shù)及其取值如下表1~3
表1 rc參數(shù)名稱及其取值
| rc參數(shù) | 解釋 | 取值 |
| lines.linewidth | 線條寬度 | 取0~10的數(shù)值,默認(rèn)1.5 |
| lines.linestyle | 線條樣式 | 取“-”“--”“-.”“:”4種,默認(rèn)為“-” |
| lines.marker | 線條上點(diǎn)的形狀 | 可取“o”“D”等20種,默認(rèn)為None |
| lines,markersize | 點(diǎn)的大小 | 取0~10的數(shù)值,默認(rèn)為1 |
表2 線條樣式lines.linestyle的取值 linestyle取值意義linestyle取值意義-實(shí)線-.點(diǎn)線--長(zhǎng)虛線:短虛線
| linestyle取值 | 意義 | linestyle取值 | 意義 |
| - | 實(shí)線 | -. | 點(diǎn)線 |
| -- | 長(zhǎng)虛線 | : | 短虛線 |
表3 lines.marker參數(shù)的取值
| marker取值 | 意義 | marker取值 | 意義 |
| 'o' | 圓圈 | '.' | 點(diǎn) |
| 'D' | 菱形 | 's' | 正方形 |
| 'h' | 六邊形1 | '*' | 星號(hào) |
| 'H' | 六邊形2 | 'd' | 小菱形 |
| '-' | 水平線 | 'v' | 一角朝下的三角形 |
| '8' | 八邊形 | '<' | 一角朝左的三角形 |
| 'p' | 五邊形 | '>' | 一角朝右的三角形 |
| ',' | 像素 | '^' | 一角朝上的三角形 |
| '+' | 加號(hào) | '|' | 豎線 |
| 'None' | 無(wú) | 'x' | X |
需要注意的是,由于默認(rèn)的Pyplot字體并不支持中文符的顯示,因此需要通過(guò)設(shè)置font.sans-serif參數(shù)改變繪圖時(shí)的字體,使得圖形可以正常顯示中文。同時(shí),由于更換字體后,會(huì)導(dǎo)致坐標(biāo)軸中的部分字符無(wú)法顯示,因此需要同時(shí)更改axes.unicode_minus參數(shù)。
plt.rcParams['font.family'] = ['SimHei'] #用來(lái)顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus'] = False #用來(lái)正常顯示符號(hào)
如果需要在坐標(biāo)軸上顯示時(shí)間,可以利用DateFormatter提供的功能進(jìn)行設(shè)置,常用代碼如下:
from matplotlib.dates import DateFormatter
plt.gca().xaxis.set_major_formatter(DateFormatter('%y/%m/%d'))
#自動(dòng)旋轉(zhuǎn)X軸的刻度,適應(yīng)坐標(biāo)軸
plt.gcf().autofmt_xdate()除了設(shè)置線條的字體的rc參數(shù)外,還有設(shè)置文本、箱線圖、坐標(biāo)軸、刻度、圖例、標(biāo)記、圖片、圖像保存等rc參數(shù)。
例1.rc參數(shù)設(shè)置例1
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
#配置中文顯示
plt.rcParams['font.family'] = ['SimHei'] #用來(lái)顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False #用來(lái)正常顯示符號(hào)
def f(t):
return np.cos(2*np.pi*t)
x1 = np.arange(0.0,4.0,0.5)
x2 = np.arange(0.0,4.0,0.01)
plt.figure(1)
plt.subplot(2,2,1)
plt.plot(x1,f(x1),'bo',x2,f(x2),'k')
plt.title('子圖1')
plt.subplot(2,2,2)
plt.plot(x2,f(x2),'r--')
plt.title('子圖2')
plt.show()OUT:

例2.rc參數(shù)設(shè)置例2
fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three') ax.legend(loc = 'best')
OUT:

可以用set_xticks設(shè)置X軸刻度
例3.用set_xticks設(shè)置刻度
fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three') ax.set_xticks([0,5,10,15,20,25,30,35]) ax.legend(loc = 'best')
OUT:

可以用set_xticklabels改變刻度,設(shè)置刻度的旋轉(zhuǎn)角度及字體等。
例4.用set_xticklabels改變刻度
fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two') ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three') ax.set_xticklabels(['x1','x2','x3','x4','x5'],rotation = 30,fontsize = 'large') ax.legend(loc = 'best')
OUT:

其中,rotation參數(shù)表示X坐標(biāo)標(biāo)簽的旋轉(zhuǎn)角度;fontsize為字號(hào),可以取值為“xx-small”“x-small”“small”“medium”“large”“x-large”“xx-large”“smaller”“None”。
3.繪圖的填充
3.1調(diào)用函數(shù)fill_between()實(shí)現(xiàn)曲線下面部分的填充
x = np.linspace(0,1,500) y = np.sin(3*np.pi*x)*np.exp(-4*x) fig,ax = plt.subplots() plt.plot(x,y) plt.fill_between(x,0,y,facecolor = 'green',alpha = 0.3)

其中,參數(shù)x表示整個(gè)X軸都覆蓋;0表示覆蓋的下限;y表示覆蓋的上限時(shí)y這條曲線,facecolor表示覆蓋區(qū)域的顏色;alpha表示覆蓋區(qū)域的透明度[0,1],其值越大,表示越不透明
3.2 部分區(qū)域填充
plt.fill_between(x[15:300],0,0.4,facecolor = 'green',alpha = 0.3)

3.3 兩條曲線之間的區(qū)域填充
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,500) y1 = np.sin(3*np.pi*x)*np.exp(-4*x) y2 = y1 + 0.2 plt.plot(x,y1,'b') plt.plot(x,y2,'r') plt.fill_between(x,y1,y2,facecolor = 'green',alpha = 0.3) plt.show()

3.4 直接使用fill進(jìn)行繪圖的填充
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,500) y = np.sin(3*np.pi*x)*np.exp(-4*x) fig,ax = plt.subplots() ax.fill(x,y) plt.show()

到此這篇關(guān)于python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充的文章就介紹到這了,更多相關(guān)python設(shè)置Pyplot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)賬號(hào)密碼輸錯(cuò)三次即鎖定功能簡(jiǎn)單示例
這篇文章主要介紹了Python實(shí)現(xiàn)賬號(hào)密碼輸錯(cuò)三次即鎖定功能,結(jié)合實(shí)例形式分析了Python文件讀取、流程控制、數(shù)據(jù)判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Pytorch GPU顯存充足卻顯示out of memory的解決方式
今天小編就為大家分享一篇Pytorch GPU顯存充足卻顯示out of memory的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
使用Python和Pygame輕松實(shí)現(xiàn)播放音頻播放器
在這個(gè)數(shù)字化時(shí)代,音頻和音樂(lè)已成為我們?nèi)粘I畹囊徊糠?不管是為了放松、學(xué)習(xí)還是工作,一個(gè)好的音樂(lè)播放器總是必不可少的,所以本文給大家介紹了用Python和Pygame制作自己的音頻播放器,感興趣的朋友可以參考下2024-01-01
簡(jiǎn)單了解Django ContentType內(nèi)置組件
這篇文章主要介紹了簡(jiǎn)單了解Django ContentType內(nèi)置組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python3 利用requests 庫(kù)進(jìn)行post攜帶賬號(hào)密碼請(qǐng)求數(shù)據(jù)的方法
今天小編就為大家分享一篇Python3 利用requests 庫(kù)進(jìn)行post攜帶賬號(hào)密碼請(qǐng)求數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
用Python3創(chuàng)建httpServer的簡(jiǎn)單方法
今天小編就為大家分享一篇用Python3創(chuàng)建httpServer的簡(jiǎn)單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
詳解pyqt中解決國(guó)際化tr()函數(shù)不起作用的問(wèn)題
本文主要介紹了pyqt中解決國(guó)際化tr()函數(shù)不起作用的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

