简体   繁体   English

如何定义 matplotlib 图形的纵横比?

[英]How to define the aspect ratio of a matplotlib figure?

Python produces an aspect ratio that is suitable for its content eg, respects the structure of the font of each label, axis title, etc. This is the basic code using Jupyter Notebook: Python 生成适合其内容的纵横比,例如,尊重每个标签的字体结构、轴标题等。这是使用 Jupyter Notebook 的基本代码:

fig, ax = plt.subplots()
ax.boxplot(dataLipid)
ax.set_title("Lipid contact analysis")
plt.xticks([1,2,3,4,5],["x4 Monomers","x2 Monomers\nDimer","x2 Dimers","Monomer\nTrimer", "x4mer"])
plt.show()

However, I want to save the image as a tiff, with a dpi of 600, and a width of 8.3cm (maximum height is an A4 page, but the nature of my question will make that irrelevant).但是,我想将图像保存为 tiff,dpi 为 600,宽度为 8.3 厘米(最大高度是 A4 页面,但我的问题的性质将使其无关紧要)。

I'm using the code:我正在使用代码:

fig.savefig("bar.tiff", dpi=600, format="tiff", pil_kwards = {"compression":"tiff_lzm"})

This produces the following:这会产生以下结果:

在此处输入图片说明

All good so far.到目前为止一切都很好。 Next, the Royal Soc.接下来是皇家足球俱乐部。 of Chemistry expect a single column image to be 8.3 cm in width (height, no more than the page).化学期望单列图像的宽度为 8.3 厘米(高度,不超过页面)。

My question: Is there any way for Python to calculate the height of the figure given only the wdith, whilst maintaining the correct aspect ratio for the fonts, titles and ticks etc.?我的问题: Python 有什么方法可以计算仅给出宽度的图形的高度,同时保持字体、标题和刻度等的正确纵横比? If I specify width=height, the image looks terrible:如果我指定宽度=高度,图像看起来很糟糕:

fig.set_size_inches(3.26,3.26)
fig.savefig("bar.tiff", dpi=600, format="tiff", pil_kwards = {"compression":"tiff_lzm"})

在此处输入图片说明

Or is this a case where I define the size of the figure first, then adjust the font sizes as a separate step?或者这是我首先定义图形大小,然后作为单独的步骤调整字体大小的情况? I'm looking more for a one-fix solution as I have multiple figures of different size requirements (all being dpi=600 though) to produce.我正在寻找更多的单一解决方案,因为我有多个不同尺寸要求的数字(尽管都是 dpi=600)来生产。 Thanks.谢谢。

Here you go:干得好:

dataLipid = np.random.uniform(0,1,(100,5)) * 90000

fig, ax = plt.subplots()
ax.boxplot(dataLipid)

ax.set_title("Lipid contact analysis")
plt.xticks([1,2,3,4,5],["x4 Monomers","x2 Monomers\nDimer","x2 Dimers","Monomer\nTrimer", "x4mer"])
fig.set_size_inches(3.26,3.26)

# rotate ticks
plt.xticks(rotation=45)
# set bottom margin
plt.subplots_adjust(left=0.2, bottom=0.3)

fig.savefig("bar.tiff", dpi=600, format="tiff", pil_kwards = {"compression":"tiff_lzm"})

There is no general solution as far as I know.据我所知,没有通用的解决方案。 So setting the correct margin depends on your content and your data.因此,设置正确的边距取决于您的内容和数据。 Rotating the ticks is always a good option to make them readable in case of close spacing.旋转刻度始终是一个不错的选择,可以让它们在间距很近的情况下可读。

在此处输入图片说明

You can use the Axes.set_aspect method.您可以使用Axes.set_aspect方法。

# square plot
ax.set_aspect(1)

Also have a look at the tight_layout method to ensure everything is redrawn to fit in the figure.还可以查看tiny_layout方法,以确保重新绘制所有内容以适合图形。

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

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