Python Matplotlib 调整图形边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18619880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:14:45  来源:igfitidea点击:

Matplotlib adjust figure margin

pythonmatplotlib

提问by wdg

The following code gives me a plot with significant margins above and below the figure. I don't know how to eliminate the noticeable margins. subplots_adjustdoes not work as expected.

下面的代码给了我一个图,上面和下面都有显着的边距。我不知道如何消除明显的边距。subplots_adjust没有按预期工作。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
ax.set_aspect('equal')
plt.tight_layout()

tight_layouteliminates some of the margin, but not all of the margins.

tight_layout消除了一些边距,但不是所有的边距。

What I wanted is actually setting the aspect ratio to any customized value and eliminating the white space at the same time.

我想要的实际上是将纵横比设置为任何自定义值并同时消除空白。

Update: as Pierre H. puts it, the key is to change the size of the figure container. So my question is: Could you suggest a way to accommodate the size of the figure to the size of the axes with arbitrary aspect ratio?

更新:正如 Pierre H. 所说,关键是改变图形容器的大小。所以我的问题是:您能否提出一种方法来将图形的大小与具有任意纵横比的轴的大小相适应?

In other words, first I create a figure and an axes on it, and then I change the size of the axes (by changing aspect ratio for example), which in general will leave a portion of the figure container empty. At this stage, we need to change the size of the figure accordingly to eliminate the blank space on the figure container.

换句话说,首先我创建一个图形和一个轴,然后我更改轴的大小(例如通过更改纵横比),这通常会使图形容器的一部分留空。在这个阶段,我们需要相应地改变图形的大小,以消除图形容器上的空白空间。

回答by Pierre H.

I think the subplot_adjustcall is irrelevant here since the adjustment is overridden by tight_layout. Anyway, this only change the size of the axesinside the figure.

我认为subplot_adjust调用在这里无关紧要,因为调整被tight_layout. 无论如何,这只会改变图形内的大小。

As tcaswell pointed it, you need to change the size of the figure. Either at creation (my proposition below) or after, using fig.set_size_inches. I'm here creating a figure with a 1:1 aspect ratio using the figsize=(6,6)argument (of course 6 inches is an arbitrary choice):

正如 tcaswell 指出的那样,您需要更改figure大小。在创建时(我的命题如下)或之后,使用fig.set_size_inches. 我在这里使用figsize=(6,6)参数创建了一个纵横比为 1:1 的图形(当然 6 英寸是一个任意选择):

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
ax.set_aspect('equal')
plt.tight_layout()

回答by kevin

回答by Nuno Aniceto

After plotting your chart you can easily manipulate margins this way:

绘制图表后,您可以通过以下方式轻松操作边距:

plot_margin = 0.25

x0, x1, y0, y1 = plt.axis()
plt.axis((x0 - plot_margin,
          x1 + plot_margin,
          y0 - plot_margin,
          y1 + plot_margin))

This example could be changed to the aspect ratio you want or change the margins as you really want. In other stacktoverflow posts many questions related to margins could make use of this simpler approach.

此示例可以更改为您想要的纵横比或根据您的实际需要更改边距。在其他 stacktoverflow 帖子中,许多与边距相关的问题都可以使用这种更简单的方法。

Best regards.

此致。

回答by Minh

I just discovered how to eliminate all margins from my figures. I didn't use tight_layout(), instead I used:

我刚刚发现了如何从我的数字中消除所有边距。我没有使用tight_layout(),而是使用了:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20,20))
ax = plt.subplot(111,aspect = 'equal')
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

Hope this helps.

希望这可以帮助。

回答by mason

You should use add_axesif you want exact control of the figure layout. eg.

add_axes如果您想精确控制图形布局,则应使用。例如。

left = 0.05
bottom = 0.05 
width = 0.9
height = 0.9
ax = fig.add_axes([left, bottom, width, height])

回答by beahacker

I think what you need is, and it works well for me.

我认为你需要的是,它对我来说效果很好。

plt.axis('tight')

This command will automatically scale the axis to fit tightly to the data. Also check the answer of Nuno Aniceto for a customized axis. The documents are in https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.axis.

此命令将自动缩放轴以紧密贴合数据。另请查看 Nuno Aniceto 的答案以获得自定义轴。文档位于https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.axis 中

Be aware that

意识到

plt.savefig(filename, bbox_inches='tight')

will help remove white space of all the figure including labels, etc, but not the white space inside the axes.

将有助于删除所有图形的空白区域,包括标签等,但不包括轴内的空白区域。

回答by YASUR

You can use like: plt.subplots_adjust(wspace=1, hspace=0.5,left=0.1,top=0.9,right=0.9,bottom=0.1) And delete the item:bbox_inches='tight' in plt.savefig().

您可以使用类似: plt.subplots_adjust(wspace=1, hspace=0.5,left=0.1,top=0.9,right=0.9,bottom=0.1) 并删除 plt.savefig() 中的 item:bbox_inches='tight'。