减少matplotlib图中的左右边距

问题描述 投票:150回答:8

我正在努力处理matplotlib中的情节边距。我使用下面的代码生成我的图表:

plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")

但是,我得到了一个输出数字,在图的两侧有很多空白区域。我搜索了谷歌并阅读matplotlib文档,但我似乎无法找到如何减少这一点。

python matplotlib
8个回答
205
投票

自动执行此操作的一种方法是bbox_inches='tight' kwarg to plt.savefig

EG

import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')

另一种方法是使用fig.tight_layout()

import matplotlib.pyplot as plt
import numpy as np

xs = np.linspace(0, 1, 20); ys = np.sin(xs)

fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)

# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')

123
投票

您可以使用subplots_adjust()函数调整matplotlib数字周围的间距:

import matplotlib.pyplot as plt
plt.plot(whatever)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)

这将适用于屏幕上的图形并保存到文件中,即使您在一个图形上没有多个图形,它也是正确的调用功能。

数字是图形尺寸的分数,需要调整以允许图形标签。


47
投票

所有你需要的是

plt.tight_layout()

输出之前。

除了缩小边距外,这还可以将任何子图之间的空间紧密分组:

x = [1,2,3]
y = [1,4,9]
import matplotlib.pyplot as plt
fig = plt.figure()
subplot1 = fig.add_subplot(121)
subplot1.plot(x,y)
subplot2 = fig.add_subplot(122)
subplot2.plot(y,x)
fig.tight_layout()
plt.show()

7
投票

如果你想要精确控制图形布局,只需使用ax = fig.add_axes([left, bottom, width, height])。例如。

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

4
投票
plt.savefig("circle.png", bbox_inches='tight',pad_inches=-1)

3
投票

matplotlib subplots_adjust的问题是你输入的值是相对于图的x和y figsize。此示例用于打印pdf的正确图形大小:

为此,我重新计算相对间距为绝对值,如下所示:

pyplot.subplots_adjust(left = (5/25.4)/figure.xsize, bottom = (4/25.4)/figure.ysize, right = 1 - (1/25.4)/figure.xsize, top = 1 - (3/25.4)/figure.ysize)

对于x维度中'figure.xsize'英寸的数字和y维度中'figure.ysize'英寸的数字。因此,整个图形的左边距为5毫米,底边距为4毫米,右边为1毫米,顶部为3毫米。完成(x / 25.4)的转换是因为我需要将mm转换为英寸。

请注意,x的纯图表大小为“figure.xsize - left margin - right margin”,y的纯图表大小为“figure.ysize - bottom margin - top margin”,单位为英寸

其他片段(不确定这些,我只是想提供其他参数)

pyplot.figure(figsize = figureSize, dpi = None)

pyplot.savefig("outputname.eps", dpi = 100)

3
投票

受Sammys上述答案的启发:

margins = {  #     vvv margin in inches
    "left"   :     1.5 / figsize[0],
    "bottom" :     0.8 / figsize[1],
    "right"  : 1 - 0.3 / figsize[0],
    "top"    : 1 - 1   / figsize[1]
}
fig.subplots_adjust(**margins)

figsize是你在fig = pyplot.figure(figsize=...)中使用的元组


2
投票

对我来说,上面的答案不适用于Win7上的matplotlib.__version__ = 1.4.3。因此,如果我们只对图像本身感兴趣(即,如果我们不需要注释,轴,刻度,标题,ylabel等),那么最好将numpy数组保存为图像而不是savefig

from pylab import *

ax = subplot(111)
ax.imshow(some_image_numpyarray)
imsave('test.tif', some_image_numpyarray)

# or, if the image came from tiff or png etc
RGBbuffer = ax.get_images()[0].get_array()
imsave('test.tif', RGBbuffer)

另外,使用opencv绘图函数(cv2.line,cv2.polylines),我们可以直接在numpy数组上做一些绘图。 http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html

© www.soinside.com 2019 - 2024. All rights reserved.