-
只有一个子图时
plt.figure() #默认画布大小 plt.figure( figsize=(width,height) ) #自定义画布大小(width,height) plt.plot(...) #使用plt绘图 -
有多个子图时(但在一张画布上)
- 方法1:使用add_subplot
# 用 2x2 个子图为例 fig = plt.figure( [figsize=(width,height)] ) #定义整个画布 ax1 = fig.add_subplot(221) #第一个子图 ax1.plot(...) #在子图上作图 ax2 = fig.add_subplot(222) ax2.plot(...) ax3 = fig.add_subplot(223) ax3.plot(...) ax4 = fig.add_subplot(224) ax4.plot(...)- 方法2:使用subplots
# 仍用 2x2 个子图为例 fig,axes = plt.subplots( 2,2, [figsize=(width,height)] ) ax = axes.flatten() ax[0].plot(...) #若不flatten axes,则这里用axes[0,0] ax[1].plot(...) ...
更新的内容:
-
调整子图间距
plt.tight_layout()

本文介绍如何使用Python的Matplotlib库创建单个及多个子图,并详细解释了两种方法:add_subplot和subplots的使用技巧。同时,还提供了调整子图间距的方法以优化图表展示效果。
2424





