binned_statistic_2d产生意外的负值

时间:2015-11-10 21:55:04

标签: python scipy

我使用scipy.stats.binned_statistic_2d然后绘制输出。当我使用stat="count"时,我没有问题。当我使用stat="mean"(或np.max()这个问题)时,我最终会在每个bin中显示负值(由颜色条标识),但不应该是这样,因为我构造了{{ 1}}这样它总是大于零。有谁知道为什么会这样?我已经包含了用于生成绘图的最小代码。我还得到一个无效的值zvals警告,这让我觉得RunTime中发生了一些奇怪的事情。以下代码应该只复制并运行。

来自文档:

binned_statistic_2d

这让我相信'count' : compute the count of points within each bin. This is identical to an unweighted histogram. `values` array is not referenced. 可能会发生一些事情,以及它如何处理z值。

binned_statistic_2d

这是运行时警告:

import numbers as _numbers
import numpy as _np
import scipy as _scipy
import matplotlib as _mpl
import types as _types

import scipy.stats

from matplotlib import pyplot as _plt

norm_args = (0, 3, int(1e5)) # loc, scale, size

x = _np.random.random(norm_args[-1])  # xvals can be log scaled.
y = _np.random.normal(*norm_args) #_np.random.random(norm_args[-1]) #
z = _np.abs(_np.random.normal(1e2, *norm_args[1:]))

nbins = 1e2
kwargs = {}
stat = _np.max


fig, ax = _plt.subplots()

binned_stats = _scipy.stats.binned_statistic_2d(x, y, z, stat,
                                                nbins)
H, xedges, yedges, binnumber = binned_stats


Hplot = H
if isinstance(stat, str): 
    cbar_title = stat.title()
elif isinstance(stat, _types.FunctionType):
    cbar_title = stat.__name__.title()

XX, YY = _np.meshgrid(xedges, yedges)
Image   = ax.pcolormesh(XX, YY, Hplot.T) #norm=norm,
ax.autoscale(tight=True)

grid_kargs = {'orientation': 'vertical'}
cax, kw    = _mpl.colorbar.make_axes_gridspec(ax, **grid_kargs)
cbar       = fig.colorbar(Image, cax=cax)
cbar.set_label(cbar_title)

图片的意思是: enter image description here

最大图片: enter image description here

带计数的图片: enter image description here

1 个答案:

答案 0 :(得分:0)

原来问题是与plt.pcolormesh接口。我必须将输出数组从binned_statistic_2d转换为掩盖NaN的掩码数组。

这是给我答案的问题: pcolormesh with missing values?

相关问题