【发布时间】:2022-10-15 01:53:02
【问题描述】:
如果您有两个子图,如何仅更改第二个图的长度?
将figsize=(7,5) 添加到plt.subplots 会更改两个图的大小,但希望仅更改第二个图的大小。
源代码:https://www.w3schools.com/python/matplotlib_subplot.asp
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x1,y1)
#plot 2: how to make the second plot longer? picturing both plots have the same width, but the 2nd plot being longer
x2 = np.array([0, 1, 2, 3])
y2 = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x2,y2)
plt.show()
# alternative way to plot
fig, (ax1, ax2) = plt.subplots(1, 2,figsize=(7,5))
ax1.plot(x1,y1)
ax2.plot(x2,y2)
plt.show()
没有错误,但是您如何仅调整第二个图的大小但保持图的位置彼此相邻?谢谢!
【问题讨论】:
-
您需要
width_ratios关键字参数(参见 here),例如plt.subplots(2, 1, width_ratios=(1, 2)) -
谢谢!当我将
width_ratios应用于子图时,我不幸收到错误__init__() got an unexpected keyword argument 'width_ratios'...。你知道那是从哪里来的吗?或者你能举个简单的例子说明在哪里申请width_ratios?非常感谢! -
width_ratios是个好建议,但它在matplotlib.gridspec模块中可用,而不是subplots
标签: python numpy matplotlib jupyter subplot