matplotlib共享坐標(biāo)軸的實(shí)現(xiàn)(X或Y坐標(biāo)軸)
前言
1. 概述
共享坐標(biāo)軸就是幾幅子圖之間共享x軸或y軸,這一部分主要了解如何在利用matplotlib制圖時(shí)共享坐標(biāo)軸。
pyplot.subplots(nrows = 1,ncols = 1,sharex = False,sharey = False, squeeze = True,subplot_kw =無,gridspec_kw =無,** fig_kw )
參數(shù):
nrows:行數(shù)
ncols:列數(shù)
sharex:是否共享X軸坐標(biāo)
sharey:是否共享Y軸坐標(biāo)
返回值:Figure,Axes對象數(shù)組
一、sharex和sharey 代碼示例:
''' 1. 程序目的: 基于sharex和sharey實(shí)現(xiàn) (1) 共享x軸 (2) 共享y軸 (3) 同時(shí)共享x軸和y軸 (4) 調(diào)整子圖之間的距離 2. 版本 2.1 山東青島 2021年5月18日 Version 1 ''' # 1. 相關(guān)模塊導(dǎo)入 import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 正常顯示中文字體 plt.rcParams['axes.unicode_minus'] = False # 正常顯示負(fù)號 # 2. 創(chuàng)建制圖數(shù)據(jù) x = np.linspace(-5,5,100) y_1 = np.sin(x) y_2 = np.cos(x) y_3 = y_2*2 # 3. 繪圖 # 3.1 共享X軸 figure,(ax1,ax2,ax3) = plt.subplots(3,1, figsize=(5,6), dpi=600, # 共享x軸 sharex=True) ax1.plot(x,y_1,c='blue',linestyle=':') ax2.plot(x,y_2,c='orange',linestyle=':') ax3.plot(x,y_3,c='r',linestyle=':') # 調(diào)整子圖形之間的縱向距離 figure.subplots_adjust(hspace=0.1) ax1.set_title('以下三圖共享了X軸') # 其實(shí)更合理的添加圖名時(shí)figure.subtitle() # 3.2 共享Y軸 # 創(chuàng)建新的繪圖figure和axes對象 figure,(ax1,ax2,ax3) = plt.subplots(1,3, figsize=(6,2), dpi=600, # 共享y軸 sharey=True) figure.suptitle('以下三圖共享了Y軸') ax1.plot(x,y_1,c='blue',linestyle=':') ax2.plot(x,y_2,c='orange',linestyle=':') ax3.plot(x,y_3,c='r',linestyle=':') # 調(diào)整子圖形之間的橫向距離 figure.subplots_adjust(wspace=0.1) # 3.3 同時(shí)共享x軸和y軸 # 創(chuàng)建新的繪圖figure和axes對象 figure,(ax1,ax2,ax3) = plt.subplots(1,3, figsize=(6,2), dpi=600, # 共享x軸 sharex=True, # 共享y軸 sharey=True) x4 = np.linspace(-10,10,100) y_4 = np.cos(x4)*2 figure.suptitle('以下三圖同時(shí)共享了X軸和Y軸') ax1.plot(x,y_1,c='blue',linestyle=':') ax2.plot(x,y_2,c='orange',linestyle=':') ax3.plot(x4,y_4,c='r',linestyle=':') # 調(diào)整子圖形之間的橫向距離 figure.subplots_adjust(wspace=0.1) plt.show()
制圖結(jié)果:
實(shí)例2
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False x=np.linspace(0,2*np.pi,500) y=np.sin(x)*np.exp(-x) fig,ax=plt.subplots(nrows=1,ncols=2,sharey=True) ax1=ax[0] ax1.plot(x,y) ax1.set_title("折線圖") ax2=ax[1] ax2.scatter(x,y) ax2.set_title("散點(diǎn)圖") plt.suptitle("一張畫布兩個(gè)子圖,并共享y坐標(biāo)") #刪除空隙wspace為兩圖的水平距離,hspace為兩圖的垂直距離 fig.subplots_adjust(wspace=0) plt.show()
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False x=np.linspace(0,2*np.pi,500) y=np.sin(x)*np.exp(-x) fig,ax=plt.subplots(nrows=1,ncols=1) ax.plot(x,y) ax.set_title("折線圖") ax.scatter(x,y[::-1]) plt.suptitle("共享單一繪圖區(qū)域的坐標(biāo)軸") plt.show()
到此這篇關(guān)于matplotlib共享坐標(biāo)軸的實(shí)現(xiàn)(X或Y坐標(biāo)軸)的文章就介紹到這了,更多相關(guān)matplotlib共享坐標(biāo)軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python matplotlib坐標(biāo)軸設(shè)置的方法
- Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔以及刻度范圍
- Python利用matplotlib.pyplot繪圖時(shí)如何設(shè)置坐標(biāo)軸刻度
- Python繪圖Matplotlib之坐標(biāo)軸及刻度總結(jié)
- python使用Matplotlib改變坐標(biāo)軸的默認(rèn)位置
- 對python中Matplotlib的坐標(biāo)軸的坐標(biāo)區(qū)間的設(shè)定實(shí)例講解
- python matplotlib畫盒圖、子圖解決坐標(biāo)軸標(biāo)簽重疊的問題
- Matplotlib自定義坐標(biāo)軸刻度的實(shí)現(xiàn)示例
- Python利用matplotlib做圖中圖及次坐標(biāo)軸的實(shí)例
相關(guān)文章
Pytorch搭建YoloV4目標(biāo)檢測平臺(tái)實(shí)現(xiàn)源碼
這篇文章主要為大家介紹了Pytorch搭建YoloV4目標(biāo)檢測平臺(tái)實(shí)現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05caffe的python接口繪制loss和accuracy曲線
這篇文章主要為大家介紹了caffe的python接口繪制loss和accuracy曲線示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

Python操作SQLite數(shù)據(jù)庫過程解析

django數(shù)據(jù)模型(Model)的字段類型解析

為了順利買到演唱會(huì)的票用Python制作了自動(dòng)搶票的腳本