2010-10-28 331 views
116

我正在努力處理我在matplotlib中的繪圖邊距。我用下面的代碼生成圖表我:減少matplotlib圖中的左右邊距

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

不過,我得到有很多的空白對劇情的兩側輸出的身影。我搜索谷歌和閱讀matplotlib文檔,但我似乎無法找到如何減少此。

+0

使用的元組的問題在'imshow'的'extent'空格數量figure,或由'savefig'生成的數字周圍的png中的邊界空白的數量? – unutbu 2010-10-28 14:45:52

+0

我認爲兩者 - 在觀看窗口和PNG中似乎都有很大的空間。然而,重要的輸出是'savefig'生成的png文件 - 所以這就是我想要分類的東西。 – robintw 2010-10-28 15:36:30

+0

我剛剛在GIMP中裁剪過它們。 :/ – endolith 2011-11-28 17:42:24

回答

160

自動執行此操作的一種方法是使用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') 
+4

有什麼辦法可以使這個默認? – endolith 2012-07-08 01:08:06

+1

如果你有多個子圖並且想要保存它們中的每一個,你也可以在'fig.savefig()'中使用它。 ('plt.savefig()'在這種情況下不起作用)。 – 2013-04-21 12:07:18

+1

所有這些都會在渲染完成後裁剪圖像;如果您試圖執行特定的分辨率,圖像將會變小*。 – detly 2015-01-19 11:23:21

93

您可以使用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) 

這兩個圖上工作屏幕並保存到文件中,即使您在一個圖中沒有多個圖,它也是正確的調用功能。

這些數字是圖形尺寸的幾分之一,需要調整以允許使用圖形標籤。

+6

分配給參數的值,而不是改變多少,它們是在哪裏設置的保證金。換句話說,如果你想把右邊緣的邊距增加10%,你應該設置右邊= 0.9,而不是右邊= 0.1 http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplots_adjust – drootang 2011-11-18 16:18:50

+1

有意義的是指出,顯然你可以在plt.subplots_adjust()中指定負值。這樣做甚至可以讓您在圖形區域之外繪製圖像,並處理令人討厭的邊距。 – surchs 2014-02-12 15:02:20

+0

這也適用於調用'update'方法的'GridSpec'對象(參見http://stackoverflow.com/a/20058199/1030876)。 – 2017-02-19 21:44:06

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

「pad_inches = -1」導致我的savefig只生成部分圖。 – 2013-07-13 23:34:27

+0

在'savefig'函數中使用參數很好用,但是'pad_inches'的負值並不一定都需要。 – MichaelHuelsen 2017-07-26 07:17:34

+0

將它設置爲0,幫助 – Joop 2018-03-08 12:25:54

3

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) 
+3

你從哪裏得到'xsize'和'ysize'。我使用這些屬性,我得到'AttributeError:'圖'對象沒有屬性'xsize'' – cj5 2014-05-16 14:56:37

+0

看到這裏:http://stackoverflow.com/a/43502756/4473230 – theCake 2017-04-19 17:45:07

37

所有你需要的是

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() 
+5

我認爲這真的是最好的方法。它不需要像'bbox ='tight'那樣保存圖形,並且在狹小的圖形中修復所有其他佈局問題。 – dshepherd 2013-04-30 23:59:15

+1

這應該是正確的答案,因爲它的行爲與你期望的一樣,因爲它適用於圖而不是圖像。 – 2015-11-12 18:47:54

1

對我來說,答案上面沒有與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

5

如果你想精確控制圖形佈局,只需使用「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]) 
2

通過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=...)