1. 概述
'''
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ù)號(hào)
# 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對(duì)象
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對(duì)象
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()
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!