3、matplotlib 子图

本文详细介绍Matplotlib中的不同布局方法,包括subplot、axes及GridSpec的使用技巧,通过具体实例展示了如何利用这些方法来创建多样化的子图布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

matplotlib.pyplot.subplot

  • subplot(行数, 列数, 第n个图)
import matplotlib.pyplot as mp

# mp.figure(facecolor='lightgray')
for i in range(6):
    mp.subplot(2, 3, i+1)
    mp.xticks(())  # x轴置空
    mp.yticks(())  # y轴置空
    # .text(x, y, s, fontdict=None, withdash=False, **kwargs)
    mp.text(.5, .5, str(i+1), ha='center', va='center', size=36, alpha=0.5)

mp.tight_layout()
mp.show()

matplotlib.pyplot.axes

  • axes([左距, 下距, 宽, 高])
import matplotlib.pyplot as mp
mp.figure(facecolor='lightgray', figsize=(2, 2))

mp.axes([0.05, 0.05, 0.9, 0.9])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36, alpha=0.5)

mp.axes([0.1, 0.1, 0.3, 0.3])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36, alpha=0.5)

mp.show()

这里写图片描述

matplotlib.gridspec.GridSpec

  • GridSpec(行数, 列数)
import matplotlib.pyplot as mp
import matplotlib.gridspec as mg
# 创建figure对象
mp.figure(facecolor='lightgray')
# 3行3列
gs = mg.GridSpec(3, 3)

for i in range(3):
    for j in range(3):
        # 传入参数
        mp.subplot(gs[i, j])
        mp.xticks(())
        mp.yticks(())
        mp.text(0.5, 0.5, str(i)+str(j), ha='center', va='center', size=36, alpha=0.5)
mp.tight_layout()
mp.show()

这里写图片描述

import matplotlib.pyplot as mp
import matplotlib.gridspec as mg
# 创建figure对象
mp.figure(facecolor='lightgray')
# 3行3列
gs = mg.GridSpec(3, 3)

# 子图1
mp.subplot(gs[0, :2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36, alpha=0.5)
# 子图2
mp.subplot(gs[1:, 0])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36, alpha=0.5)
# 子图3
mp.subplot(gs[2, 1:])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '3', ha='center', va='center', size=36, alpha=0.5)
# 子图4
mp.subplot(gs[:2, 2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '4', ha='center', va='center', size=36, alpha=0.5)
# 子图5
mp.subplot(gs[1, 1])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '5', ha='center', va='center', size=36, alpha=0.5)

# 防挤压
mp.tight_layout()
# 展示
mp.show()

这里写图片描述

Appendix

EnCn
grid网格
specifyvt. 指定;详细说明;
axesn. 轴线
tickn. 滴答声;记号;
figure图形
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小基基o_O

您的鼓励是我创作的巨大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值