2012-06-01 3 views
1

matplotlib에 배경으로 사진을 넣으려고합니다. 나는 273 x 272 픽셀 크기의 사진을 추가 할 수있다. 그런 다음 30 x 30 크기의 등고선 플롯을 추가합니다. 사진을 플롯하는 선을 주석 처리하면 등고선 플롯이 전체 플롯 영역을 포함합니다.그림의 맨 위에 PyQt matplotlib 플롯 윤곽 데이터 - 스케일링 문제

사진을 포함하면 윤곽 플롯이 왼쪽 하단에 나타납니다. 그것은 각 축을 따라 전체 캔버스의 약 30/272에 해당하는 부분에 플롯되어있는 것처럼 보입니다. 내가 원하는 것은 컨투어 플롯이 전체 사진을 덮도록하는 것입니다. 제대로 맞도록

# Matplotlib Figure object 
from matplotlib.figure import Figure 

# import the Qt4Agg FigureCanvas object, that binds Figure to 
# Qt4Agg backend. It also inherits from QWidget 
from matplotlib.backends.backend_qt4agg \ 
import FigureCanvasQTAgg as FigureCanvas 

from PIL import Image 

..... 

class Qt4ContourCanvas(FigureCanvas): 
    def __init__(self, Z_matrix, plot_freq, p2_freq, p2_power, ws_level, p2_patch_on, pmin, pmax, my_alpha, parent=None): 

     global p2_frequency 
     logger.debug("%s - created" % self.__class__.__name__) 
     self.fig = Figure(facecolor='Lavender') 
     self.axes = self.fig.add_subplot(111) 

     #Reduce the size of the borders 
     #http://stackoverflow.com/questions/1203639/how-do-i-limit-the-border-size-on-a-matplotlib-graph 
     self.fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=1-0.05, 
       wspace=0.01, hspace=0.01) 

     # We need to keep a class variable of Z to prevent it going out of scope 
     self.Z = Z_matrix 

............ 

    def drawContourPlot(self, Z_matrix, plot_freq, p2_freq, p2_power, ws_level, p2_patch_on, pmin, pmax, my_alpha): 
     "Method to plot contour plots" 
     global p2_frequency 
     p2_frequency = p2_freq 
     self.axes.cla() 

     self.Z = Z_matrix 


     map_dimensions = Z_matrix.shape 
     my_xdim = map_dimensions[0] 
     my_ydim = map_dimensions[1] 

     levels = np.arange(pmin, pmax, 2.5) 

     DIM = len(self.Z) 
     x = y = np.arange(0, DIM, 1) 
     X, Y = np.meshgrid(x, y) 

     my_cm = ListedColormap(faramir_cm) 

     # Background picture 
     picture = Image.open('gondor.png') 
     CSbkgr = self.axes.imshow(picture, origin='lower') 


     # Swap X and Y to transpose the data, otherwise the click event 
     # and the matrix coordinates do not agree 
     CS = self.axes.contourf(Y, X, self.Z, levels, cmap=my_cm, alpha=my_alpha) 

     CS2 = self.axes.contour(CS, levels=CS.levels, colors = 'r', hold='on') 
     self.axes.clabel(CS2, fontsize=10, inline=1, fmt='%1.1f') 

     CS3 = self.axes.contour(CS, levels=[ws_level], colors = 'black', hold='on', linestyles = 'solid', linewidths = 2) 

     self.axes.clabel(CS3, fontsize=12, inline=1, fmt='%1.1f') 

     self.axes.grid(True, color='white') 

     self.fig.canvas.draw() 

답변

1

당신은 당신의 윤곽 플롯 크기를 조정 : 대신 enter image description here (왼쪽 아래에서 색 점을

코드 (안 완전한 작동 예)의 관련 부분입니다 이 enter image description here 코드 :

import Image 
import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 

#contour plot test data: 
delta = 0.025 
x = np.arange(-3.0, 3.0, delta) 
y = np.arange(-2.0, 2.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) 
# difference of Gaussians 
Z = 10.0 * (Z2 - Z1) 

plt.figure() 
im = Image.open('tree_small.png') 
plt.imshow(im, origin='lower') 

#rescale contour plot: 
X = X - np.min(X) 
X = X * im.size[0]/np.max(X) 
Y = Y - np.min(Y) 
Y = Y * im.size[1]/np.max(Y) 
plt.contour(X, Y, Z, 20) 

plt.show() 

은 아마 당신이 사기를 중첩 할 수 있습니다 코너 스케일 없음의 윤곽 플롯 ...)입니다 별도의 축을 사용하여 둘러보기 계획을 세웠지 만 가장 빠른 방법 인 것 같습니다.)

+0

감사합니다. – user1430729

관련 문제