Matplotlib 调整图形边距 [英] Matplotlib adjust figure margin
问题描述
以下代码为我提供了一个在图的上方和下方均具有显着空白的图.我不知道如何消除明显的利润.subplots_adjust
没有按预期工作.
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_adjust
does not work as expected.
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_layout
消除了一些空白,但并没有消除所有空白.
tight_layout
eliminates some of the margin, but not all of the margins.
我想要的实际上是将纵横比设置为任何自定义值,并同时消除空白.
What I wanted is actually setting the aspect ratio to any customized value and eliminating the white space at the same time.
更新:正如Pierre H.所说,关键是更改图形容器的大小.所以我的问题是:您能否建议一种以任意纵横比将图形的大小容纳到轴的大小的方法?
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?
换句话说,首先创建一个图形和一个轴,然后更改轴的大小(例如,通过更改宽高比),这通常会将图形容器的一部分留空.在这个阶段,我们需要相应地改变图形的大小,以消除图形容器上的空白.
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.
推荐答案
我刚刚发现了如何消除图形中的所有边距.我没有使用 tight_layout()
,而是使用了:
I just discovered how to eliminate all margins from my figures. I didn't use tight_layout()
, instead I used:
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)
希望这会有所帮助.
这篇关于Matplotlib 调整图形边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!