matplotlib关于 figure 和 subplot 使用

本文详细介绍了使用Matplotlib库在Python中绘制多子图的四种不同方法,包括使用figure.add_subplot、subplots、figure.subplots及plt.subplot,每种方法都通过实例展示了如何创建并显示多个子图。

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

import matplotlib.pyplot as plt
# 方法一
fig1 = plt.figure()
ax = fig1.add_subplot(1, 2, 1)
ax.plot(range(10), range(10))
ax2 = fig1.add_subplot(1, 2, 2)
ax2.plot(range(10), range(10))
fig1.show()

# 方法二
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
axes[0,0].plot(range(2), range(1, 3))
axes[0,1].plot(range(3), range(2, 5))
axes[1,0].plot(range(4), range(3, 7))
axes[1,1].plot(range(5), range(4, 9))
fig.show()

# 方法三
fig = plt.figure()
axes = fig.subplots(2, 2)
axes[0,1].plot(range(2), range(1,3))
axes[0,0].plot(range(3), range(2,5))
fig.show()

# 方法四
ax1=plt.subplot(2, 2, 1)
ax2=plt.subplot(2, 2, 2)
ax3=plt.subplot(2, 2, 3)
ax4=plt.subplot(2, 2, 4)
ax1.plot(range(10), range(10))
ax2.plot(range(10), range(10))
ax3.plot(range(10), range(10))
ax4.plot(range(10), range(10))
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值