-
plt.subplot2grid
-
效果
-
代码
在程序中通过注释进行说明
# 通过plot.subplotgrid()来划分网格 import matplotlib.pyplot as plt plt.figure('subgrid') ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) # 生成子图对象 # 其中(3,3)表示将整个画布分成 3行*3列 的网格布局 # (0,0)表示占据索引(索引从0开始)为(0,0)的方格 # colspan 列扩展,=3即占3列 # plot()画折线图 ax1.plot([1, 2], [1, 2]) # 画小图 ax1.set_title('ax1_title') # 设置小图的标题 ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, title='ax2') ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2, title='ax3') # rowspan 行扩展,=2即占2行 ax4 = plt.subplot2grid((3, 3), (2, 0), title='ax4') ax5 = plt.subplot2grid((3, 3), (2, 1), title='ax5') # scatter()画散点图 ax4.scatter([1, 2], [2, 2]) # 设置x轴,y轴的轴属性说明 ax4.set_xlabel('ax4_x') ax4.set_ylabel('ax4_y') # 加上这一句可以避免不同子图的边缘重叠(在有label,title时可能会发生) plt.tight_layout() plt.show()
-
-
gridspec.GridSpec
-
效果
-
代码
在程序中通过注释进行说明
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec plt.figure('gridspec.GridSpec') gs = gridspec.GridSpec(3, 3) # 将画布分成3行*3列的网格布局 # 利用切片选取若干网格画子图 # 对于[0:2,1:-1] # 其中逗号(,)用来隔离维度,冒号(:)用来连接切片的起始索引和终止索引 # 表示在第1个维度上,取第0行到第1行;在第2个维度上取第1列到倒数第二列(-1表示倒数第一项); # 嗯,这里的切片是含首不含尾的,这种切片机制在python中普遍存在 # 只有一个冒号(:)就表示去这一维度的所有项 ax6 = plt.subplot(gs[0, :], title='ax6') ax7 = plt.subplot(gs[1, :2], title='ax7') ax8 = plt.subplot(gs[1:, 2], title='ax8') ax9 = plt.subplot(gs[-1, 0], title='ax9') ax10 = plt.subplot(gs[-1, -2], title='ax10') # 加上这一句可以避免不同子图的边缘重叠(在有label,title时可能会发生) plt.tight_layout() plt.show()
-
-
plt.subplots
-
效果
-
代码
在代码中通过注释说明
import matplotlib.pyplot as plt figure, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True) # 生成2*2的分布布局 # sharex, sharey 共享x轴,y轴 # 散点图 ax11.scatter([1,2], [1,2]) # 折线图 ax12.plot([1,2], [2,1]) plt.tight_layout() plt.show()
-
-
说明
三种方式每种可在一个py文件中单独执行
-
参考文献
程序主要来自 Subplot 分格显示,略有改动
Matplotlib多子图显示2——网格划分
猜你喜欢
转载自blog.csdn.net/BBJG_001/article/details/104439299
今日推荐
周排行