简体   繁体   English

使用tight_layout() 使轴居中

[英]Centering Axes using tight_layout()

All,全部,

I'm trying to export figures in (roughly) a particular size so I can include them in high-res in my LaTeX document.我正在尝试以(大致)特定尺寸导出数字,以便可以将它们以高分辨率包含在我的 LaTeX 文档中。 When I draw the figure, the ylabel is cutoff (I assume because my figure is small, 2.7in wide).当我绘制图形时,ylabel 被截断(我假设因为我的图形很小,2.7 英寸宽)。 When I call tight_layout(), I get the labels fine, but now the axes are no longer center in the saved image.当我调用tight_layout() 时,标签很好,但现在轴不再位于保存图像的中心。 I need the axes centered above the caption, so I want the axes centered on the image.我需要以标题上方为中心的轴,所以我希望轴以图像为中心。

I tried adding a second axis to the right side, but I couldn't figure out how to make the labels and ticks invisible for that axis.我尝试在右侧添加第二个轴,但我不知道如何使该轴的标签和刻度不可见。

Here is without tight_layout()这里没有tight_layout()

默认

Here is with tight_layout()这里是tight_layout()

紧密布局()

Any idea how I can get the best of both worlds (everything visible, with the axes centered)?知道如何才能两全其美(一切可见,以轴为中心)吗?

Thanks!谢谢!

It would be nice to test it on the code.在代码上测试它会很好。 But try playing with axes.set_position() as shown here: https://www.google.com/amp/s/www.geeksforgeeks.org/matplotlib-axes-axes-set_position-in-python/amp/但是尝试使用axes.set_position()如下所示: https://www.google.com/amp/s/www.geeksforgeeks.org/matplotlib-axes-axes-set_position-in-python/amp/

I also encountered this problem and it seems there is no elegent way after I googled it.我也遇到过这个问题,google了一下好像没有什么好办法。 I ended up with the fig.subplots_adjust .我最终得到了fig.subplots_adjust

import matplotlib.pyplot as plt

fig = plt.figure()
plt.plot([10,20,30])

plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.patch.set_facecolor('silver')
fig.set_size_inches(w=3.5, h =3)
# %%
#fig.tight_layout()
plt.subplots_adjust(left=0.15, right=0.85, bottom=0.15, top=0.9)
fig.savefig('test.png')

But this will produce additional padding around the axes and tedious parameter-tuning...但这会在轴周围产生额外的填充和繁琐的参数调整......

在此处输入图像描述

while fig.tight_layout() produces:fig.tight_layout()产生:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM