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()
matplotlib关于 figure 和 subplot 使用
Matplotlib多子图绘制技巧
最新推荐文章于 2024-11-09 18:23:08 发布
本文详细介绍了使用Matplotlib库在Python中绘制多子图的四种不同方法,包括使用figure.add_subplot、subplots、figure.subplots及plt.subplot,每种方法都通过实例展示了如何创建并显示多个子图。

9187

被折叠的 条评论
为什么被折叠?



