Matplotlib 固定图形大小和子图位置



我正在打扰你一个关于matplotlib中子图大小的问题。我需要创建一个固定大小的图形,由单行中的 3 个子图组成。出于"编辑原因",我需要固定图形的大小,但我也想在不影响图形大小的情况下固定子图的大小和位置(第三个子图必须比前两个子图窄(。

我尝试使用GridSpec但没有成功。我还尝试使用"figsize"固定图形大小,并对子图使用add_axes,但是,根据子图的相对大小,图形和子图的整体大小会发生变化。

使用 gnuplot 时,可以使用"集合原点"和"设置大小"作为子图。我们在 matplotlib 中有类似的东西?

像这样的东西?更改宽度比将更改各个子图的大小。

fig, [ax1, ax2, ax3] = plt.subplots(1,3, gridspec_kw = {'width_ratios':[3, 2, 1]}, figsize=(10,10))
plt.show()

如果您想对大小进行更多控制,也可以使用轴。它仍然是相对的,但现在是整个图形大小的一小部分。

import matplotlib.pyplot as plt
# use plt.Axes(figure, [left, bottom, width, height])
# where each value in the frame is between 0 and 1
# left
figure = plt.figure(figsize=(10,3))
ax1 = plt.Axes(figure, [.1, .1, .25, .80])
figure.add_axes(ax1)
ax1.plot([1, 2, 3], [1, 2, 3])
# middle
ax2 = plt.Axes(figure, [.4, .1, .25, .80])
figure.add_axes(ax2)
ax2.plot([1, 2, 3], [1, 2, 3])
# right
ax3= plt.Axes(figure, [.7, .1, .25, .80])
figure.add_axes(ax3)
ax3.plot([1, 2, 3], [1, 2, 3])
plt.show()

最新更新