2017-10-06 81 views
1

我有一個類似如下(散點圖)一些衛星數據:用於計數scipy.stats.binned_statistic_2d工作,但並不意味着

Night-time Ion Density

我現在想斌這個數據到規則的網格上時間和緯度,並且每個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) 

所得的情節

Counts

現在,如果我改變統計意味着,np.mean,np.ma.mean等......這是陰謀,我得到這似乎挑選出的地方有數據和那裏是沒有:

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() 

count_working_code mean_working_code

編輯3:User8153能夠找出問題所在。解決的辦法是從scipy統計數據中屏蔽出現nans的數組。我用np.ma.masked_invalid()來做到這一點。我的原始數據和測試數據的平均值低於平均統計量。

Working Mean My Data Working Mean Sample Data

+0

更換使用你掩蓋了具有計數0,即H'的'這些元素的''count''統計數據,沒有數據。根據'binned_statistic_2d'的文檔,當將統計數據更改爲「mean」或「median」時,空格表示爲'NaN'。你是否嘗試改變面具以過濾掉那些「NaN」? – user8153

+0

可能相關:https://github.com/matplotlib/matplotlib/issues/6069/ – user8153

+1

你檢查過NA值嗎?您尚未提供您的數據,因此無法重現。 – denfromufa

回答

1

當使用binned_statistic_2d空箱的'count'統計被標記爲爲零,你在你的代碼掩蓋。如果您切換到'mean''median'統計信息,則空箱將由NaN表示,因此您必須調整該掩碼。要做到這一點的方法之一是

H = np.ma.masked_where(H==0, H) 

通過

H = np.ma.masked_invalid(H)