使用 matplotlib 标签时创建对称图形

Create symmetric figures when using matplotlib labels

我将 plt.subplotsax.set_ylabelfig.supylabel 一起使用。但是,这会创建偏离中心的图形。

是否可以自动增加右边距,使红线位于图形的中心? 在我手动执行此操作的情况下,如何精确衡量应该增加多少右边距?

这个怎么样:

fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
x = np.arange(50)
ax1.plot(x,np.sin(x))
ax2.plot(x,np.sin(x))
ax1.set_ylim(-1,1)
ax2.set_ylim(-1,1)
ax2.set_yticklabels('')
ax1.set_title('damped')
ax2.set_title('undamped')
ax1.set_ylabel('amplitude')
fig.suptitle('Different types of oscillations')

输出:

---编辑---

试试这个:

import matplotlib.gridspec as grd

fig = plt.subplots()

gs = grd.GridSpec(1, 2, wspace=0.5)
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

x = np.arange(50)
ax1.plot(x,np.sin(x))
ax2.plot(x,np.sin(x))

ax1.set_title('damped')
ax2.set_title('undamped')
ax1.set_ylabel('amplitude')

关键点是gs = grd.GridSpec(1, 2, wspace=0.5)。根据需要调整 wspace。下面的图是 wspace=0.5