减少matplotlib图中的左右边距
我正在努力处理 matplotlib 中的绘图边距。我使用下面的代码来生成我的图表:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
但是,我得到的输出图在图的两侧都有大量空白。我搜索了谷歌并阅读了 matplotlib 文档,但我似乎找不到如何减少这种情况。
I'm struggling to deal with my plot margins in matplotlib. I've used the code below to produce my chart:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
However, I get an output figure with lots of white space on either side of the plot. I've searched google and read the matplotlib documentation, but I can't seem to find how to reduce this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
自动执行此操作的一种方法是将
bbox_inches='tight'
kwarg 转换为plt.savefig
。例如,
另一种方法是使用
fig。 strict_layout()
One way to automatically do this is the
bbox_inches='tight'
kwarg toplt.savefig
.E.g.
Another way is to use
fig.tight_layout()
您可以使用 subplots_adjust() 函数调整 matplotlib 图形周围的间距:
这适用于屏幕上的图形和保存到文件中的图形,即使您在一个图形上没有多个图形,它也是正确调用的函数数字。
这些数字是图形尺寸的分数,需要进行调整以允许图形标签。
You can adjust the spacing around matplotlib figures using the subplots_adjust() function:
This will work for both the figure on screen and saved to a file, and it is the right function to call even if you don't have multiple plots on the one figure.
The numbers are fractions of the figure dimensions, and will need to be adjusted to allow for the figure labels.
您所需要的只是
在输出之前。
除了减少边距之外,这还紧密地分组了所有子图之间的空间:
All you need is
before your output.
In addition to cutting down the margins, this also tightly groups the space between any subplots:
有时,
plt.tight_layout()
无法提供最佳视图或我想要的视图。那么为什么不先用任意边距绘制并在绘制后修复边距呢?因为我们从那里得到了很好的所见即所得。
然后将设置粘贴到边距函数中以使其永久化:
Sometimes, the
plt.tight_layout()
doesn't give me the best view or the view I want. Then why don't plot with arbitrary margin first and do fixing the margin after plot?Since we got nice WYSIWYG from there.
Then paste settings into margin function to make it permanent:
如果有人想知道如何在应用
plt.tight_layout()
或fig.tight_layout()
后如何消除其余的白边距:使用参数pad
(默认情况下为1.08
),您可以使其更紧:“图形边缘和子图边缘之间的填充,作为字体大小的一部分。”
例如,
将其减少到很小的幅度。输入
0
对我来说不起作用,因为它也会使子图的框被切掉一点。In case anybody wonders how how to get rid of the rest of the white margin after applying
plt.tight_layout()
orfig.tight_layout()
: With the parameterpad
(which is1.08
by default), you're able to make it even tighter:"Padding between the figure edge and the edges of subplots, as a fraction of the font size."
So for example
will reduce it to a very small margin. Putting
0
doesn't work for me, as it makes the box of the subplot be cut off a little, too.只需使用 ax = Fig.add_axes([left, Bottom, width, height])
如果您想精确控制图形布局。例如。
Just use
ax = fig.add_axes([left, bottom, width, height])
if you want exact control of the figure layout. eg.
对于最新的 matplotlib 版本,您可能需要尝试约束布局:
太糟糕了 pandas 处理不好...
With recent matplotlib versions you might want to try Constrained Layout:
Too bad pandas does not handle it well...
受到 Sammys 上面答案的启发:
其中 Figsize 是您在
fig = pyplot.figure(figsize=...)
中使用的元组inspired by Sammys answer above:
Where figsize is the tuple that you used in
fig = pyplot.figure(figsize=...)
matplotlibs subplots_adjust 的问题是您输入的值相对于图形的 x 和 y Figsize。此示例用于正确调整 pdf 打印的图形大小:
为此,我重新计算绝对值的相对间距,如下所示:
对于 x 维度中的“figure.xsize”英寸和 y 维度中的“figure.ysize”英寸的图形方面。因此,整个图的左边距为 5 毫米,下边距为 4 毫米,右边距为 1 毫米,顶部边距为 3 毫米,放置标签。完成 (x/25.4) 的转换是因为我需要将毫米转换为英寸。
请注意,x 的纯图表大小将为“figure.xsize - 左边距 - 右边距”,y 的纯图表大小将为“figure.ysize - 下边距 - 上边距”,以英寸为单位
其他片段(不确定这些)那些,我只是想提供其他参数)
和
The problem with matplotlibs subplots_adjust is that the values you enter are relative to the x and y figsize of the figure. This example is for correct figuresizing for printing of a pdf:
For that, I recalculate the relative spacing to absolute values like this:
for a figure of 'figure.xsize' inches in x-dimension and 'figure.ysize' inches in y-dimension. So the whole figure has a left margin of 5 mm, bottom margin of 4 mm, right of 1 mm and top of 3 mm within the labels are placed. The conversion of (x/25.4) is done because I needed to convert mm to inches.
Note that the pure chart size of x will be "figure.xsize - left margin - right margin" and the pure chart size of y will be "figure.ysize - bottom margin - top margin" in inches
Other sniplets (not sure about these ones, I just wanted to provide the other parameters)
and
对我来说,上面的答案不适用于 Win7 上的
matplotlib.__version__ = 1.4.3
。因此,如果我们只对图像本身感兴趣(即,如果我们不需要注释、轴、刻度、标题、ylabel 等),那么最好将 numpy 数组保存为图像,而不是 savefig< /代码>。另外,使用opencv绘图函数(cv2.line、cv2.polylines),我们可以直接在numpy数组上进行一些绘图。 http://docs.opencv.org/2.4/modules/core/doc /drawing_functions.html
For me, the answers above did not work with
matplotlib.__version__ = 1.4.3
on Win7. So, if we are only interested in the image itself (i.e., if we don't need annotations, axis, ticks, title, ylabel etc), then it's better to simply save the numpy array as image instead ofsavefig
.Also, using opencv drawing functions (cv2.line, cv2.polylines), we can do some drawings directly on the numpy array. http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
用于子图的紧凑 matplotlib 模板
由于每次设置新图时我都会遇到同样的麻烦,因此我决定在 stackoverflow 上记录一个模板。
模板
output.png
compact matplotlib template for subplots
As I have the same hussle each time I set up a new plot, I decided to document a template for this on stackoverflow.
template
output.png