【Python画图03】多图显示

一、学习背景

最近在学习python制图,准备做个模板好出图

二、参考

这个学习系列都参考了B站UP主的视频《【莫烦Python】Matplotlib Python 画图教程》,我是将他的代码重新输入下来后加入了自己的注释和理解,感恩

三、代码学习

1. 四图合一,都是正方形的图

import matplotlib.pyplot as plt
import numpy as np


plt.figure()

plt.subplot(2,2,1) #将画布分成2行2列的四个,现在将第1个设为活动画布,在上面绘图
plt.plot([0,1],[0,1]) #准备画的线两头x分别为(0,0)和(1,1)

plt.subplot(2,2,2) #将画布分成2行2列的四个,现在将第1个设为活动画布,在上面绘图
plt.plot([0,1],[0,2]) #准备画的线两头x分别为(0,0)和(1,2)

plt.subplot(2,2,3) #将画布分成2行2列的四个,现在将第1个设为活动画布,在上面绘图
plt.plot([0,1],[0,3]) #准备画的线两头x分别为(0,0)和(1,3)

plt.subplot(2,2,4) #将画布分成2行2列的四个,现在将第1个设为活动画布,在上面绘图
plt.plot([0,1],[0,4]) #准备画的线两头x分别为(0,0)和(1,4)


plt.show()

在这里插入图片描述

方法2 创建2乘2的网格

import matplotlib.pyplot as plt
import numpy as np

#创建一个2*2图像的网格
f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[1,2]) #第1行第1列的网格画散点图

plt.tight_layout()
plt.show()

在这里插入图片描述

方法3:设置长方形的图像合集,分格显示,第一行设置为一列,第二行设置为三列,索引按照小的来

这一种方法大致是这样的:
在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np


plt.figure()

plt.subplot(2,1,1) #将画布分成2行1列的四个,现在将第1个设为活动画布,在上面绘图
plt.plot([0,1],[0,1]) #准备画的线两头x分别为(0,0)和(1,1)

plt.subplot(2,3,4) #将画布分成2行3列的6个,现在将第4个设为活动画布,在上面绘图
plt.plot([0,1],[0,2]) #准备画的线两头x分别为(0,0)和(1,2)

plt.subplot(2,3,5) #将画布分成2行3列的6个,现在将第5个设为活动画布,在上面绘图
plt.plot([0,1],[0,3]) #准备画的线两头x分别为(0,0)和(1,3)

plt.subplot(2,3,6) #将画布分成2行3列的6个,现在将第6个设为活动画布,在上面绘图
plt.plot([0,1],[0,4]) #准备画的线两头x分别为(0,0)和(1,4)


plt.show()

在这里插入图片描述

方法4:设置长方形的图像合集,组合正方形为长方形

这一种和下一种的划分是一样的,但是索引不同:
在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np


plt.figure()
#将画布分为3*3个格子,ax1从第1列第1行开始(第二个括号为索引)占据3列1行,
ax1=plt.subplot2grid((3,3), (0,0), colspan=3,rowspan=1)
ax1.plot([1,2],[1,2])
ax1.set_title('This is ax1')
#将画布分为3*3个格子,ax2从第2行第1列开始(第二个括号为索引)占据2列1行,
ax2=plt.subplot2grid((3,3), (1,0), colspan=2,rowspan=1)
ax2.plot([1,3],[1,3])
ax2.set_title('This is ax2')
ax3=plt.subplot2grid((3,3), (1,2), colspan=1,rowspan=2)
ax4=plt.subplot2grid((3,3), (2,0), colspan=1,rowspan=1)
ax5=plt.subplot2grid((3,3), (2,1), colspan=1,rowspan=1)

plt.show()

在这里插入图片描述

方法5:设置长方形的图像合集,划分大格网

plt.figure()
gs=gridspec.GridSpec(3,3) #划分3*3的网格
ax1=plt.subplot(gs[0,:]) #ax1为占据第1行,所有列的网格
ax2=plt.subplot(gs[1,:2]) #ax2为占据第2行,占据第1列至第2列的网格
ax3=plt.subplot(gs[1:,2]) #ax3为占据第2行至最后一行,占据第2列的网格
ax4=plt.subplot(gs[-1,0]) #ax4为占据最后一行,占据第1列的网格
ax5=plt.subplot(gs[-1,-2]) #ax5为占据最后一行,占据倒数第二列的网格


plt.show()

在这里插入图片描述

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Matplotlib 库来画图并设置多个的大小,下面是一个简单的示例代码: ```python import matplotlib.pyplot as plt # 创建一个 2x2 的像 fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8)) # 在第一个子画图并设置大小 axes[0, 0].plot([1, 2, 3], [4, 5, 6]) axes[0, 0].set_title('Plot 1') axes[0, 0].set_xlabel('X label') axes[0, 0].set_ylabel('Y label') axes[0, 0].set_xlim([0, 4]) axes[0, 0].set_ylim([3, 7]) axes[0, 0].set_aspect('equal') axes[0, 0].figure.set_size_inches(4, 4) # 在第二个子画图并设置大小 axes[0, 1].plot([1, 2, 3], [4, 5, 6]) axes[0, 1].set_title('Plot 2') axes[0, 1].set_xlabel('X label') axes[0, 1].set_ylabel('Y label') axes[0, 1].set_xlim([0, 4]) axes[0, 1].set_ylim([3, 7]) axes[0, 1].set_aspect('equal') axes[0, 1].figure.set_size_inches(4, 4) # 在第三个子画图并设置大小 axes[1, 0].plot([1, 2, 3], [4, 5, 6]) axes[1, 0].set_title('Plot 3') axes[1, 0].set_xlabel('X label') axes[1, 0].set_ylabel('Y label') axes[1, 0].set_xlim([0, 4]) axes[1, 0].set_ylim([3, 7]) axes[1, 0].set_aspect('equal') axes[1, 0].figure.set_size_inches(4, 4) # 在第四个子画图并设置大小 axes[1, 1].plot([1, 2, 3], [4, 5, 6]) axes[1, 1].set_title('Plot 4') axes[1, 1].set_xlabel('X label') axes[1, 1].set_ylabel('Y label') axes[1, 1].set_xlim([0, 4]) axes[1, 1].set_ylim([3, 7]) axes[1, 1].set_aspect('equal') axes[1, 1].figure.set_size_inches(4, 4) plt.show() ``` 在上面的代码中,我们首先使用 `plt.subplots()` 函数创建一个 2x2 的像,然后在每个子画图并设置大小。在这里,我们使用 `figure.set_size_inches()` 方法来设置每个子的大小。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值