子图的布局与共享子图/(非)相邻子图的坐标轴

子图的绘制、绘制固定区域、绘制自定义区域的子图、子图坐标轴的共享、子图的布局

共享子图的坐标轴

当pyplot使用subplots()函数绘制子图时,可以通过sharex或sharey参数控制是否共享x轴或y轴。sharex或sharey参数支持False或‘none’、True或‘all’、‘row’(表示每一行的子图之间共享x轴或y轴)、‘col’(表示每一列的子图之间共享x轴或y轴)中任取一值。

import numpy as np
import matplotlib.pyplot as plt 
#1.数据准备
x1=np.linspace(0,2*np.pi,400)
y1=np.cos(x1**2)

x2=np.linspace(0.01,10,100)
y2=np.sin(x2)

#2.创建坐标系实例【两个坐标系实例共享轴--两两之间共享】
ax1=plt.subplot(2,2,1)
#ax2与ax1两个坐标系实例共享x轴
ax2=plt.subplot(2,2,4,   sharex=ax1)
ax3=plt.subplot(2,2,3,   sharex=ax2)
#3.绘图
ax1.plot(x1,y1)
ax2.plot(x2,y2)
ax3.plot(x2,y2)
#4.图标展示
plt.show()

运行结果:

共邻非相邻子图的坐标图

import numpy as np
import matplotlib.pyplot as plt 
#数据准备
x1=np.linspace(0,2*np.pi,400)
y1=np.cos(x1**2)
x2=np.linspace(0.01,10,100)
y2=np.sin(x2)
ax_one=plt.subplot(221)
ax_one.plot(x1,y1)
#共享子图ax_one和ax_two的x轴
ax_two=plt.subplot(224,sharex=ax_one)
ax_two.plot(x2,y2)
#展示
plt.show

运行展示:

 

当个子图也可以共享坐标轴,它通常会将y轴作为一组图形参考的坐标轴,将右侧的垂直坐标作为另一组图形参考的坐标轴。---twinx(ax=none)

fig,ax=plt.subplots()

ax_right=ax.twinx()

子图的布局

matplotlib中提供了一些调整了一些调整子图布局的方法,包括约束布局、紧密布局和自定义布局。

约束布局:plt.subplot(constrained_layout=none),紧密布局:tight-layout(pad=1.08,h
_pad=None,w_pad=None,rect=None---(left,bottom,right,top),自定义布局函数:GridSpec(nrows,ncols,figure=None,left=None,bottom=None,right=None,top=None,
wspace=None,hspace=None,width_ratios=None,height_ratios=None)
约束布局:
import numpy as np
import matplotlib.pyplot as plt 
#绘制子图并启用约束布局
fig,axs=plt.subplots(2,2,constrained_layout=True)
ax1=axs[0,0]
ax1.set_title('title')
ax2=axs[0,1]
ax2.set_title('title')
ax3=axs[1,0]
ax3.set_title('title')
ax4=axs[1,1]
ax4.set_title('title')
plt.show()

紧密布局:

import numpy as np
import matplotlib.pyplot as plt 
#绘制子图并启用紧密布局
fig,axs=plt.subplots(2,2)
ax1=axs[0,0]
ax1.set_title('title')
ax2=axs[0,1]
ax2.set_title('title')
ax3=axs[1,0]
ax3.set_title('title')
ax4=axs[1,1]
ax4.set_title('title')
#调整子图之间的距离
plt.tight_layout(pad=0.4,w_pad=0.5,h_pad=2)#h_pad,w_pad表示相邻之间的空白区域的大小,pad表示画布边缘与子图边缘之间的空白区域的大小。
plt.show()

自定义布局:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec#matplotlib的gridspec模块是专门指定画布中子图位置的模块,该模块包含了GridSpec类,通过显示地创建GridSpec类对象来自定义画布中子图的布局结构,使得子图能更好地适应画布。
fig2=plt.figure()
spec2=gridspec.GridSpec(2,2,figure=fig2)
f2_ax1=fig2.add_subplot(spec2[0,0])
f2_ax2=fig2.add_subplot(spec2[0,1])
f2_ax3=fig2.add_subplot(spec2[1,0])
f2_ax4=fig2.add_subplot(spec2[1,1])
plt.show()

add_gridspec()方法:
import matplotlib.pyplot as plt
 #使用add_gridspec()方法向画布添加布局结果
fig3 = plt.figure(constrained_layout=True)
gs = fig3.add_gridspec(3, 3)
f3_ax1 = fig3.add_subplot(gs[0, :])
f3_ax1.set_title('gs[0, :]')
f3_ax2 = fig3.add_subplot(gs[1, :-1])
f3_ax2.set_title('gs[1, :-1]')
f3_ax3 = fig3.add_subplot(gs[1:, -1])
f3_ax3.set_title('gs[1:, -1]')
f3_ax4 = fig3.add_subplot(gs[-1, 0])
f3_ax4.set_title('gs[-1, 0]')
f3_ax5 = fig3.add_subplot(gs[-1, -2])
f3_ax5.set_title('gs[-1, -2]')
plt.show()

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
#创建画布实例
fig3=plt.figure()
#通过GridSpec类的构造方法,绘制“区域规划图”实例
spec3 =gridspec.GridSpec(3, 4,figure=fig3)
#把“区域规划图”传进subplot,按照规划图,创建对应的坐标系实例
ax1= plt.subplot(spec3[0,0:4])#索引操作
ax2= plt.subplot(spec3[1,0:2])
ax3= plt.subplot(spec3[1,2:4])
ax4= plt.subplot(spec3[2,0:1])
plt.subplot(349,polar=True)
ax5= plt.subplot(spec3[2,1:4])
#调整子图的位置
plt.tight_layout()
#展示
plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值