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

显示结果:

相关推荐
万粉变现经纪人2 小时前
如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
qq_479875432 小时前
C++ std::Set<std::pair>
开发语言·c++
查士丁尼·绵3 小时前
笔试-九宫格三阶积幻方
python·九宫格·三阶积幻方
云知谷4 小时前
【C++基本功】C++适合做什么,哪些领域适合哪些领域不适合?
c语言·开发语言·c++·人工智能·团队开发
l1t5 小时前
DeepSeek辅助利用搬移底层xml实现快速编辑xlsx文件的python程序
xml·开发语言·python·xlsx
大飞记Python5 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
C_Liu_7 小时前
C++:list
开发语言·c++
my rainy days7 小时前
C++:友元
开发语言·c++·算法
小梁努力敲代码7 小时前
java数据结构--List的介绍
java·开发语言·数据结构
查士丁尼·绵7 小时前
笔试-羊狼过河
python