【Python】matplotlib分格显示

参考:matplotlib图形整合之多个子图一起绘制_matplotlib多子图_王小王-123的博客-CSDN博客

方式一:

python 复制代码
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.figure()
# 方式一: gridspec
# rowspan:行的跨度,colspan:列的跨度
# 第一格
ax1=plt.subplot2grid((3,3),(0,0), colspan=3, rowspan=1)
ax1.plot([1,2],[1,4])
ax1.set_xlabel("xlabel")
ax1.set_ylabel("ylabel")
ax1.set_title("ax1_title")
# ax1.set_xlim(0,4)
# ax1.set_ylim(0,6)
ax1.grid()
# 第二格
ax2=plt.subplot2grid((3,3),(1,0), colspan=2, rowspan=1)
# 第三格
ax3=plt.subplot2grid((3,3),(1,2), rowspan=2)
# 第四格
ax4=plt.subplot2grid((3,3),(2,0))
# 第五格
ax4=plt.subplot2grid((3,3),(2,1))

plt.tight_layout()
plt.show()

显示结果:

方式二:

python 复制代码
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# 方式二: easy to define structure
plt.figure(num=33)
gs = gridspec.GridSpec(3,3)
x11 = plt.subplot(gs[0,:])     # gs[0, :]  从第1行(索引0)开始,占了所有列(:)
x11.set_title("gs[0, :]")
x21 = plt.subplot(gs[1,:2])    # gs[1, :2] 从第2行(索引1)开始,占了前2列(:2)
x21.set_title("gs[1,:2]")
x22 = plt.subplot(gs[1:,2])    # gs[1:, 2] 从第2行(1:)开始占了之后的所有行(2,3行),占了第3列(索引2)
x22.set_title("gs[1:,2]")
x31 = plt.subplot(gs[-1,0])    # gs[-1, 0] 该子图占了第3行(索引-1),第1列(0)
x31.set_title("gs[-1,0]")
x32 = plt.subplot(gs[-1,-2])   # gs[-1, -2] 该子图占了第3行(-1),第2列(-2)
x32.set_title("gs[-1,-2]")

plt.tight_layout()
plt.show()

显示结果

方式三:

python 复制代码
import matplotlib.pyplot as plt

fig, ((ax11,ax12,ax13),(ax21,ax22,ax23),(ax31,ax32,ax33)) = plt.subplots(3,3,sharex=True,sharey=True)
ax11.scatter([1,2],[1,4])
ax12.plot([1,2,3,4],[1,4,9,16])

plt.tight_layout()
plt.show()

显示结果:

相关推荐
meilindehuzi_a1 小时前
从零理解并实现 RAG:用 LangChain.js 构建语义检索问答系统
开发语言·javascript·langchain
风流 少年1 小时前
Julia
开发语言·julia
江畔柳前堤2 小时前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
:-)2 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
GIS阵地3 小时前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211234 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长4 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
中微极客4 小时前
解锁LLM开发全栈能力:Python + LangChain + RAG 工程实战指南
人工智能·python·langchain
hhzz5 小时前
Barbershop:基于GAN和分割Mask的图像合成技术——从理论到实战全解析
图像处理·人工智能·python·深度学习·计算机视觉
charlie1145141915 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++