red*_*ger 4 python numpy matplotlib
我想用matplotlib.pyplot.hist2d绘制2d直方图.作为输入我掩盖了numpy.ma数组.这样的工作正常如下:
hist2d (arr1,arr2,cmin=1)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想规范化数组,那么我得到的值总是在0和1之间,使用normed = True关键字,就像这样
hist2d (arr1,arr2,cmin=1, normed=True)
Run Code Online (Sandbox Code Playgroud)
我收到错误
.../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)
Run Code Online (Sandbox Code Playgroud)
知道如何绕过这个仍然得到一个标准化的二维直方图吗?
因为cmin它,它并不顺利normed=True.删除cmin(或将其设置为0)将使其工作.如果你确实需要过滤,你可以考虑使用numpy的2d直方图功能,然后屏蔽输出.
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')
Run Code Online (Sandbox Code Playgroud)
