我正在努力处理我在matplotlib中的绘图边距。我用下面的代码生成图表我:减少matplotlib图中的左右边距
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
不过,我得到有很多的空白对剧情的两侧输出的身影。我搜索谷歌和阅读matplotlib文档,但我似乎无法找到如何减少此。
我正在努力处理我在matplotlib中的绘图边距。我用下面的代码生成图表我:减少matplotlib图中的左右边距
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
不过,我得到有很多的空白对剧情的两侧输出的身影。我搜索谷歌和阅读matplotlib文档,但我似乎无法找到如何减少此。
自动执行此操作的一种方法是使用bbox_inches='tight'
kwarg至plt.savefig
。
E.g.
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')
您可以使用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)
这两个图上工作屏幕并保存到文件中,即使您在一个图中没有多个图,它也是正确的调用功能。
这些数字是图形尺寸的几分之一,需要调整以允许使用图形标签。
分配给参数的值,而不是改变多少,它们是在哪里设置的保证金。换句话说,如果你想把右边缘的边距增加10%,你应该设置右边= 0.9,而不是右边= 0.1 http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplots_adjust – drootang 2011-11-18 16:18:50
有意义的是指出,显然你可以在plt.subplots_adjust()中指定负值。这样做甚至可以让您在图形区域之外绘制图像,并处理令人讨厌的边距。 – surchs 2014-02-12 15:02:20
这也适用于调用'update'方法的'GridSpec'对象(参见http://stackoverflow.com/a/20058199/1030876)。 – 2017-02-19 21:44:06
plt.savefig("circle.png", bbox_inches='tight',pad_inches=-1)
“pad_inches = -1”导致我的savefig只生成部分图。 – 2013-07-13 23:34:27
在'savefig'函数中使用参数很好用,但是'pad_inches'的负值并不一定都需要。 – MichaelHuelsen 2017-07-26 07:17:34
将它设置为0,帮助 – Joop 2018-03-08 12:25:54
matplotlibs subplots_adjust的问题是您输入的值是相对于图的x和y figsize。这个例子是对于正确figuresizing对于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 - 左边距 - 右边距”和纯图表大小Y的将是“figure.ysize - 底边距 - 上边距”以英寸
其他sniplets(不知道这些的,我只是想提供其他参数)
pyplot.figure(figsize = figureSize, dpi = None)
和
pyplot.savefig("outputname.eps", dpi = 100)
所有你需要的是
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()
我认为这真的是最好的方法。它不需要像'bbox ='tight'那样保存图形,并且在狭小的图形中修复所有其他布局问题。 – dshepherd 2013-04-30 23:59:15
这应该是正确的答案,因为它的行为与你期望的一样,因为它适用于图而不是图像。 – 2015-11-12 18:47:54
对我来说,答案上面没有与matplotlib.__version__ = 1.4.3
Win7上工作。所以,如果我们只对图像本身感兴趣(即,如果我们不需要注释,轴,刻度,标题,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
如果你想精确控制图形布局,只需使用“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])
通过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=...)
使用的元组的问题在'imshow'的'extent'空格数量figure,或由'savefig'生成的数字周围的png中的边界空白的数量? – unutbu 2010-10-28 14:45:52
我认为两者 - 在观看窗口和PNG中似乎都有很大的空间。然而,重要的输出是'savefig'生成的png文件 - 所以这就是我想要分类的东西。 – robintw 2010-10-28 15:36:30
我刚刚在GIMP中裁剪过它们。 :/ – endolith 2011-11-28 17:42:24