matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
matplotlib
默認(rèn)根據(jù)數(shù)據(jù)系列自動(dòng)縮放坐標(biāo)軸范圍。pyplot
模塊中的autoscale
函數(shù)可以切換是否自動(dòng)縮放坐標(biāo)軸范圍,xlim()
和ylim()
函數(shù)可手動(dòng)設(shè)置坐標(biāo)軸范圍。
autoscale函數(shù)
對(duì)于pyplot
模塊控制坐標(biāo)軸范圍是否自動(dòng)縮放的函數(shù)為autoscale
。
函數(shù)簽名為matplotlib.pyplot.autoscale(enable=True, axis='both', tight=None)
參數(shù)作用及取值如下:
enable
為布爾值,即是否自動(dòng)縮放。axis
取值范圍為{'both', 'x', 'y'}
,默認(rèn)值為'both'
,即作用在哪個(gè)坐標(biāo)軸。tight
為布爾值,默認(rèn)值為None
,即是否設(shè)置邊距為0。
底層相關(guān)函數(shù)有:
- Axes.autoscale
- Axes.autoscale_view
- Axes.set_autoscale_on
- Axes.get_autoscale_on
- Axes.set_autoscalex_on
- Axes.get_autoscalex_on
- Axes.set_autoscaley_on
- Axes.get_autoscaley_on
案例
自動(dòng)縮放坐標(biāo)軸范圍功能對(duì)比。
import matplotlib.pyplot as plt plt.subplot(121) plt.plot([0.5,0.5]) print(plt.gca().get_autoscale_on()) print(plt.gca().get_autoscalex_on()) print(plt.gca().get_autoscaley_on()) plt.subplot(122) plt.plot([0.5,0.5]) plt.autoscale(False) print(plt.gca().get_autoscale_on()) print(plt.gca().get_autoscalex_on()) print(plt.gca().get_autoscaley_on()) plt.show()
輸出:
True
True
True
False
False
False
xlim()函數(shù)
手動(dòng)設(shè)置x
坐標(biāo)軸的范圍或獲取x
坐標(biāo)軸的范圍。
函數(shù)簽名為matplotlib.pyplot.xlim(*args, **kwargs)
。
調(diào)用簽名有三種:
xlim((left, right))
xlim(left, right)
xlim(left=1, right=3)
其中left
為x
坐標(biāo)軸左側(cè)極值,right
為x
坐標(biāo)軸右側(cè)極值。注意!left
可以比right
大!
返回值為(left, right)
,即坐標(biāo)軸范圍元組。
xlim()
相當(dāng)于Axes.get_xlim
,xlim(*args, **kwargs)
相當(dāng)于Axes.set_xlim
。
案例
演示xlim()
的調(diào)用方法。
import matplotlib.pyplot as plt plt.figure(figsize=(14, 3)) plt.subplot(141) plt.plot([1, 1]) print(plt.xlim()) plt.subplot(142) plt.plot([1, 1]) plt.xlim(0, 1.5) plt.annotate('plt.xlim(0,1.5)', (0.1, 1.001)) print(plt.xlim()) plt.subplot(143) plt.plot([1, 1]) plt.xlim((0, 1.5)) plt.annotate('plt.xlim((0,1.5))', (0.1, 1.001)) print(plt.xlim()) plt.subplot(144) plt.plot([1, 1]) plt.xlim(left=0, right=1.5) plt.annotate('plt.xlim(left=0,right=1.5)', (0.1, 1.001)) print(plt.xlim()) plt.show()
輸出:
(-0.05, 1.05)
(0.0, 1.5)
(0.0, 1.5)
(0.0, 1.5)
ylim()函數(shù)
手動(dòng)設(shè)置y
坐標(biāo)軸的范圍或獲取y
坐標(biāo)軸的范圍。使用方法與xim()
函數(shù)相似。
函數(shù)簽名為matplotlib.pyplot.ylim(*args, **kwargs)
。
調(diào)用簽名有三種:
ylim((bottom, top))
ylim(bottom, top)
ylim(bottom=1, top=3)
其中bottom
為x
坐標(biāo)軸左側(cè)極值,top
為x
坐標(biāo)軸右側(cè)極值。注意!bottom
可以比top
大!
返回值為(bottom, top)
,即坐標(biāo)軸范圍元組。
ylim()
相當(dāng)于Axes.get_ylim
,ylim(*args, **kwargs)
相當(dāng)于Axes.set_ylim
。
案例
演示ylim()
的調(diào)用方法。
import matplotlib.pyplot as plt plt.figure(figsize=(14, 3)) plt.subplot(141) plt.plot([1, 1]) print(plt.ylim()) plt.subplot(142) plt.plot([1, 1]) plt.ylim(0, 1.5) plt.annotate('plt.ylim(0,1.5)', (0.1, 1.01)) print(plt.ylim(0,1.5)) plt.subplot(143) plt.plot([1, 1]) plt.ylim((0, 1.5)) plt.annotate('plt.ylim((0,1.5))', (0.1, 1.01)) print(plt.ylim()) plt.subplot(144) plt.plot([1, 1]) plt.ylim(bottom=0, top=1.5) plt.annotate('plt.ylim(bottom=0,top=1.5)', (0.1, 1.01)) print(plt.ylim()) plt.show()
輸出:
(0.945, 1.0550000000000002)
(0.0, 1.5)
(0.0, 1.5)
(0.0, 1.5)
到此這篇關(guān)于matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())的文章就介紹到這了,更多相關(guān)matplotlib 坐標(biāo)軸范圍內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章弄懂Python中所有數(shù)組數(shù)據(jù)類型
這篇文章主要給大家介紹了關(guān)于Python中所有數(shù)組數(shù)據(jù)類型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python進(jìn)行圖片相似度對(duì)比的兩種實(shí)現(xiàn)方法
Python提供了一些庫(kù)和工具可以用于圖片的相似度比對(duì),本文就詳細(xì)的介紹了兩種實(shí)現(xiàn)方法,感知哈希和結(jié)構(gòu)相似性,下面就來(lái)介紹一下,感興趣的可以了解一下2023-10-10使用OpenCV實(shí)現(xiàn)人臉圖像卡通化的示例代碼
這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)人臉圖像卡通化的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01windows下安裝python paramiko模塊的代碼
windows下安裝python paramiko模塊,有需要的朋友可以參考下2013-02-02Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)
這篇文章主要介紹了Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別 opencv實(shí)現(xiàn)剪刀石頭布游戲
這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別,opencv實(shí)現(xiàn)剪刀石頭布游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01基于python實(shí)現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格
這篇文章主要介紹了基于python實(shí)現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python3如何日志同時(shí)輸出到控制臺(tái)和文件
這篇文章主要介紹了Python3如何日志同時(shí)輸出到控制臺(tái)和文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11python?中的?BeautifulSoup?網(wǎng)頁(yè)使用方法解析
這篇文章主要介紹了python?中的?BeautifulSoup?網(wǎng)頁(yè)使用方法解析,文章基于python的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-04-04