用于计数scipy.stats.binned_statistic_2d工作,但并不意味着
我有一个类似如下(散点图)一些卫星数据:用于计数scipy.stats.binned_statistic_2d工作,但并不意味着
我现在想斌这个数据到规则的网格上时间和纬度,并且每个bin都等于落入其中的所有数据点的平均值。我一直在试验scipy.stats.binned_statistic_2d,并对我所得到的结果感到困惑。首先,如果我将“count”统计信息传递给scipy binning函数,它看起来可以正常工作(最小代码和下图)。
id1 = np.ma.masked_where(id1==0, id1) #id1 is the actual data and I have tried using this masking argument and without to the same effect
x_range = np.arange(0,24.25,.25) #setting grid spacing for x and y
y_range = np.arange(-13,14,1)
xbins, ybins = len(x_range), len(y_range) #number of bins in each dimension
H, xedges, yedges, binnumber = stats.binned_statistic_2d(idtime, idlat, values = id1, statistic='count' , bins = [xbins, ybins]) #idtime and idlat are the locations of each id1 value in time and latitude
H = np.ma.masked_where(H==0, H) #masking where there was no data
XX, YY = np.meshgrid(xedges, yedges)
fig = plt.figure(figsize = (13,7))
ax1=plt.subplot(111)
plot1 = ax1.pcolormesh(XX,YY,H.T)
所得的情节
现在,如果我改变统计意味着,np.mean,np.ma.mean等......这是阴谋,我得到这似乎挑选出的地方有数据和那里是没有:
即使最小值和最大值为这个数据是612和223 7026。我已经编写了一些手动执行此操作的代码,但它并不漂亮并且需要永久(并且我没有完全考虑边缘效应,因此运行到错误并修复它会一直持续)。
我希望得到这个工作的一些建议。谢谢!
编辑:我只是注意到,我运行脚本后我得到一个运行时警告,我无法找到任何有关在线信息。谷歌搜索警告返回零结果。除计数外,每个统计选项都会发出警告。
应用程序数据\本地\ Enthought \雨棚\ EDM \ ENVS \用户\ LIB \站点包\ matplotlib \ colors.py:494: RuntimeWarning:小于cbook._putmask遇到无效值(XA, XA < 0.0,-1)
编辑2:我附上了一些代码,重复我的问题。此代码适用于统计数量,但不适用于平均值或任何其他统计数据。该代码以相同的方式产生与以前相同的运行时间警告。
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
x = np.random.rand(1000)
y = np.random.rand(1000)
z = np.arange(1000)
H, xedges, yedges, binnumber = stats.binned_statistic_2d(x, y, values = z, statistic='count' , bins = [20, 20])
H2, xedges2, yedges2, binnumber2 = stats.binned_statistic_2d(x, y, values = z, statistic='mean' , bins = [20, 20])
XX, YY = np.meshgrid(xedges, yedges)
XX2, YY2 = np.meshgrid(xedges2, yedges2)
fig = plt.figure(figsize = (13,7))
ax1=plt.subplot(111)
plot1 = ax1.pcolormesh(XX,YY,H.T)
cbar = plt.colorbar(plot1,ax=ax1, pad = .015, aspect=10)
plt.show()
fig2 = plt.figure(figsize = (13,7))
ax2=plt.subplot(111)
plot2 = ax2.pcolormesh(XX2,YY2,H2.T)
cbar = plt.colorbar(plot2,ax=ax2, pad = .015, aspect=10)
plt.show()
编辑3:User8153能够找出问题所在。解决的办法是从scipy统计数据中屏蔽出现nans的数组。我用np.ma.masked_invalid()来做到这一点。我的原始数据和测试数据的平均值低于平均统计量。
当使用binned_statistic_2d
空箱的'count'
统计被标记为为零,你在你的代码掩盖。如果您切换到'mean'
或'median'
统计信息,则空箱将由NaN
表示,因此您必须调整该掩码。要做到这一点的方法之一是
H = np.ma.masked_where(H==0, H)
通过
H = np.ma.masked_invalid(H)
更换使用你掩盖了具有计数0,即H'的'这些元素的''count''统计数据,没有数据。根据'binned_statistic_2d'的文档,当将统计数据更改为“mean”或“median”时,空格表示为'NaN'。你是否尝试改变面具以过滤掉那些“NaN”? – user8153
可能相关:https://github.com/matplotlib/matplotlib/issues/6069/ – user8153
你检查过NA值吗?您尚未提供您的数据,因此无法重现。 – denfromufa