2013-08-15 4 views
4

matplotlib.pyplot.hist2d를 사용하여 2d 히스토그램을 플롯하려고합니다. 입력으로 numpy.ma 배열을 마스크했습니다. 즉, 미세 같은 같은 작품 같은 : 나는 배열을 정상화하려는, 그래서이python matplotlib 정규화 된 마스크 된 numpy 배열로 hist2d 플롯

hist2d (arr1,arr2,cmin=1, normed=True) 

I처럼 NORMED = TRUE 키워드를 사용하여, 0과 1 사이의 항상 값을 얻을 경우

hist2d (arr1,arr2,cmin=1) 

오류가 발생합니다

.../numpy/ma/core.py:3791: UserWarning: Warning: converting a masked element to nan. 
    warnings.warn("Warning: converting a masked element to nan.") 
.../matplotlib/colorbar.py:561: RuntimeWarning: invalid value encountered in greater 
    inrange = (ticks > -0.001) & (ticks < 1.001) 
.../matplotlib/colorbar.py:561: RuntimeWarning: invalid value encountered in less 
    inrange = (ticks > -0.001) & (ticks < 1.001) 
.../matplotlib/colors.py:556: RuntimeWarning: invalid value encountered in less 
    cbook._putmask(xa, xa < 0.0, -1) 

어떤 방법으로 이것을 얻을 수 있고 여전히 정규화 된 2D 히스토그램을 얻을 수 있습니까?

+0

'Cmin은 = 1':이 (* 후 * 정규화를인가하고, 모든 빈 값이 1보다 낮을 수있는 바와 같이, 히스토그램에 요소가있을 수 없다 그 값은 실제로 NaN으로 설정). 따라서, 음모에 남겨진 것은 아무것도 없습니다! – Evert

+0

예, 무슨 일이 일어나는지 설명합니다. cmin을 낮추면 다시 작동합니다 - 감사합니다. –

답변

6

cmin 때문에 normed=True과 잘 어울리지 않습니다. cmin을 제거하거나 0으로 설정하면 작동합니다. 필터링이 필요한 경우 numpy의 2 차원 히스토그램 함수를 사용하고 나중에 출력을 마스킹하는 것을 고려할 수 있습니다.

a = np.random.randn(1000) 
b = np.random.randn(1000) 

a_ma = np.ma.masked_where(a > 0, a) 
b_ma = np.ma.masked_where(b < 0, b) 

bins = np.arange(-3,3.25,0.25) 

fig, ax = plt.subplots(1,3, figsize=(10,3), subplot_kw={'aspect': 1}) 

hist, xbins, ybins, im = ax[0].hist2d(a_ma,b_ma, bins=bins, normed=True) 

hist, xbins, ybins = np.histogram2d(a_ma,b_ma, bins=bins, normed=True) 
extent = [xbins.min(),xbins.max(),ybins.min(),ybins.max()] 

im = ax[1].imshow(hist.T, interpolation='none', origin='lower', extent=extent) 
im = ax[2].imshow(np.ma.masked_where(hist == 0, hist).T, interpolation='none', origin='lower', extent=extent) 

ax[0].set_title('mpl') 
ax[1].set_title('numpy') 
ax[2].set_title('numpy masked') 

enter image description here

+0

이것은 트릭을했습니다. 나는 다음 날에 멍청한 2 차원 히스토그램에 가볼 것이다. - 감사 –

관련 문제