【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()

显示结果:

相关推荐
用户83562907805122 分钟前
使用 Python 操作 Word 内容控件
后端·python
LDR00626 分钟前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术28 分钟前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园38 分钟前
C++20 Modules 模块详解
java·开发语言·spring
swordbob1 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享2 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.2 小时前
C语言--day30
c语言·开发语言
码云骑士2 小时前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
何以解忧,唯有..2 小时前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang