【问题标题】:binned_statistic_2d producing unexpected negative valuesbinned_statistic_2d 产生意外的负值
【发布时间】:2015-11-11 11:40:21
【问题描述】:

我正在使用scipy.stats.binned_statistic_2d,然后绘制输出。当我使用stat="count" 时,我没有任何问题。当我使用stat="mean"(或np.max())时,我最终在每个箱中得到负值(由颜色条标识),这不应该是这种情况,因为我已经构造了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.

这让我相信binned_statistic_2d 中可能发生了一些事情以及它如何处理 z 值。

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)

这是运行时警告:

/Users/balterma/Library/Enthought/Canopy_64bit/User/lib/python2.7/sitepackages/matplotlib/colors.py:584: RuntimeWarning: invalid value encountered in less cbook._putmask(xa, xa < 0.0, -1)

平均图像:

最大图像:

带有计数的图像:

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    原来问题出在与plt.pcolormesh 的接口上。我必须将输出数组从 binned_statistic_2d 转换为屏蔽 NaN 的屏蔽数组。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多