2012-03-09 3 views
3

저는 matlab에 익숙해졌으며 지금은 matplotlib와 numpy로 바꾸려고합니다. matplotlib에 플로팅하는 이미지가 전체 그림 창을 차지하는 방법이 있습니까?matplotlib : 그림 전체를 덮을 늘일 때의 이미지

import numpy as np 
import matplotlib.pyplot as plt 

# get image im as nparray 
# ........ 

plt.figure() 
plt.imshow(im) 
plt.set_cmap('hot') 

plt.savefig("frame.png") 

난 그래서 입력도 정확히 동일한 크기를 savefig 않을 때 ... 도면의 크기 종횡비 및 스케일을 유지하는 화상을 원하고, 완전히 화상으로 덮여 .

감사합니다.

+0

ad_inches=0이 한 번 봐 가지고 bbox_inches='tight'savefigpad_inches=-1 또는 페이지에 매개 변수를 언급 : http://stackoverflow.com/questions/4042192/reduce- left-and-right-margins-in-matplotlib-plot –

답변

6

다음 스 니펫을 사용하여이 작업을 수행했습니다.

#!/usr/bin/env python 
import numpy as np 
import matplotlib.cm as cm 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 
from pylab import * 

delta = 0.025 
x = y = np.arange(-3.0, 3.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = Z2-Z1 # difference of Gaussians 
ax = Axes(plt.gcf(),[0,0,1,1],yticks=[],xticks=[],frame_on=False) 
plt.gcf().delaxes(plt.gca()) 
plt.gcf().add_axes(ax) 
im = plt.imshow(Z, cmap=cm.gray) 

plt.show() 

참고 양쪽 회색 테두리 aspect='equal' 또는 aspect='auto' 또는 비 설정에 의해 변경되는 축 애스펙트 rario에 관한 것이다. 또한

의견 Similar StackOverflow Question 에 Zhenya에서 언급 한 바와 같이이

+0

'bbox_inches ='tight''에 정말로 하나의 질문에 대한 답변이 플러스되었습니다. – Peaceful

1

다음과 같은 기능을 사용할 수 있습니다. 원하는 해상도로 해상도에 따라 그림에 필요한 크기 (인치)를 계산합니다.

import numpy as np 
import matplotlib.pyplot as plt 

def plot_im(image, dpi=80): 
    px,py = im.shape # depending of your matplotlib.rc you may 
           have to use py,px instead 
    #px,py = im[:,:,0].shape # if image has a (x,y,z) shape 
    size = (py/np.float(dpi), px/np.float(dpi)) # note the np.float() 

    fig = plt.figure(figsize=size, dpi=dpi) 
    ax = fig.add_axes([0, 0, 1, 1]) 
    # Customize the axis 
    # remove top and right spines 
    ax.spines['right'].set_color('none') 
    ax.spines['left'].set_color('none') 
    ax.spines['top'].set_color('none') 
    ax.spines['bottom'].set_color('none') 
    # turn off ticks 
    ax.xaxis.set_ticks_position('none') 
    ax.yaxis.set_ticks_position('none') 
    ax.xaxis.set_ticklabels([]) 
    ax.yaxis.set_ticklabels([]) 

    ax.imshow(im) 
    plt.show() 
관련 문제