在双Y图中拉伸图形大小

时间:2018-08-07 12:36:43

标签: python matplotlib figure

我正在尝试学习如何在显示(plt.show())和保存(plt.savefig)时拉伸图的宽度。

更确切地说,该图具有两个y轴,如下所示:

fig , ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x,y)
ax2.plot(x,y2)
plt.show()

现在的问题是,如何指定图的大小,例如,如何拉伸宽度/高度? 当使用plt.show()并显示绘图时,我可以手动调整一个角并根据需要调整框的大小,但是手动定制的尺寸将不会保存。

1 个答案:

答案 0 :(得分:1)

您可以使用figsize=(width, height)

设置图形尺寸比例
import matplotlib.pyplot as plt

x = [1, 2, 3]           # dummy data
y = [4, 5, 6]
y2 = [16, 25, 36]

fig , ax1 = plt.subplots(figsize=(7, 2))   # <-- aspect wider than high
ax2 = ax1.twinx()
ax1.plot(x,y)
ax2.plot(x,y2)
plt.show()