gpt4 book ai didi

python - Matplotlib - 以 A4 格式拟合所有子图

转载 作者:行者123 更新时间:2023-12-01 08:51:27 26 4
gpt4 key购买 nike

我正在尝试将 6 个子图排列在一张 A4 纵向格式中。我正在使用以下代码:

variables = ['NEE', 'Qle', 'Qh', 'SoilMoist', 'LAI']
fig = plt.figure(figsize=(8.27,11.7)) #to get A4 portrait format
for i, variable in enumerate (variables):
ax = plt.subplot2grid((5,1), (i,0))
if variable == 'NEE' or variable == 'Qh' or variable == 'Qle':
plt.plot(x,obs,c="black")
if variable == 'LAI':
plt.plot(df2['median'], c='blue')
if variable == 'SoilMoist':
plt.plot(time_ctr_ch, ctr_soil, c='lightsteelblue')
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

这提供了适合 A4 格式的所有绘图。但不幸的是,仍有大量空间未使用,如下图所示:

enter image description here

如何确保不同的子图具有相同的大小并占据 A4 纵向格式的大部分可用空间?

最佳答案

这本质上可以归结为,为什么以下行不起作用:

plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

如果我们遵循 matplotlib 正在做的事情:

plt.subplots_adjust 调用 Figure.subplots_adjust在引擎盖下。然后更新figure.SubplotParams它本身调用一个名为 update 的函数。如果我们看一下 source code有一个方便的文档字符串:

def update(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
"""
Update the dimensions of the passed parameters. *None* means unchanged.
"""

因此,因为您将 None 传递给 subplots_adjust 的参数,所以您实际上并没有更改默认值(由 matplotlibrc 文件控制)

因此,您实际上需要为参数提供浮点值,例如:

plt.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95, wspace=0, hspace=0)

或者您可以使用 plt.tight_layout

关于python - Matplotlib - 以 A4 格式拟合所有子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53088578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com