2013-08-22 2 views
3

데이터 매트릭스를 등고선으로 그립니다. 행렬의 요소 중 일부는 NaN입니다 (솔루션이없는 매개 변수 조합에 해당). 이 영역을 contourplot에 해치 영역으로 표시하고 싶습니다. 이것을 달성하는 방법에 대한 아이디어?matplotlib의 contourplot에서 NaN 영역을 해치합니다.

+0

가능한 중복 (http://stackoverflow.com/questions/14045709/

이 예제를 참조하십시오 선택 패턴과 함께 matplotlib-imshow) – tacaswell

답변

9

contourfcontour 메서드는 배열이 마스크 된 곳을 그리지 않습니다 (here 참조)! 따라서 플롯의 NaN 요소 영역을 해치로 만들려면 음영의 배경을 해치로 정의해야합니다. 당신은이 그림 얻을 것이다

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 

# generate some data: 
x,y = np.meshgrid(np.linspace(0,1),np.linspace(0,1)) 
z = np.ma.masked_array(x**2-y**2,mask=y>-x+1) 

# plot your masked array 
ax.contourf(z) 

# get data you will need to create a "background patch" to your plot 
xmin, xmax = ax.get_xlim() 
ymin, ymax = ax.get_ylim() 
xy = (xmin,ymin) 
width = xmax - xmin 
height = ymax - ymin 

# create the patch and place it in the back of countourf (zorder!) 
p = patches.Rectangle(xy, width, height, hatch='/', fill=None, zorder=-10) 
ax.add_patch(p) 
plt.show() 

: [하기 matplotlib imshow 선택적 패턴]의 enter image description here