我有一些卫星数据,如下所示(散点图):
我现在想要将这些数据随时间和纬度分成常规网格,并使每个bin等于其中所有数据点的平均值。我一直在试验scipy.stats.binned_statistic_2d,并对我得到的结果感到困惑。
首先,如果我通过"计数"统计到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和2237026。我已经编写了一些手动执行此操作的代码,但它不是很漂亮并且需要永远(并且我还没有完全考虑边缘效应,因此运行错误然后修复它将永远消失)。
我希望得到一些建议让它发挥作用。谢谢!
编辑:我刚刚注意到我在运行脚本后收到运行时警告,但我无法找到有关在线的任何信息。谷歌搜索警告返回零结果。除count之外,每个统计选项都会发出警告。
应用程序数据\本地\ Enthought \冠层\ EDM \ ENVS \用户\ lib中\站点包\ matplotlib \ colors.py:494: 运行时警告:在较少的cbook._putmask中遇到无效值(xa, xa< 0.0,-1)
Edit2:我在下面添加了一些复制我的问题的代码。此代码适用于统计计数,但不适用于均值或任何其他统计数据。此代码以相同的方式从之前生成相同的运行时警告。
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能够识别问题。解决方案是将阵列掩盖在出现nans的scipy stats中。我使用np.ma.masked_invalid()来做到这一点。我的原始数据和测试数据的图表在下面是平均统计量。
答案 0 :(得分:3)
在'count'
空箱中使用binned_statistic_2d
统计信息时,标记为零,您在代码中将其屏蔽。如果您切换到'mean'
或'median'
统计信息,则空的分箱由NaN
表示,因此您必须为此调整掩码。一种方法是替换
H = np.ma.masked_where(H==0, H)
通过
H = np.ma.masked_invalid(H)