How to use binned_statistic_2d with Non-monotonic boundary?
up vote
0
down vote
favorite
I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.
It's difficult to using binned_statistic_2d like this:
from scipy import stats
x = [-118,-117.8,-117.7,-80,-70]
y = [18.1,18,18,17.8,18]
lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
binx = lon
biny = lat
ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])
Error:
ValueError: bins must be monotonically increasing or decreasing
How to deal with this issue?
My solution
- Use
zipto get boundaries of every grid;
Add condition to make sure the order is correct
for row in np.arange(lon_b.shape[0]-1):
k = 0
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
# Because binned_statistic_2d need bin_* increase monotonically
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
k = 1
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
python scipy interpolation histogram2d
add a comment |
up vote
0
down vote
favorite
I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.
It's difficult to using binned_statistic_2d like this:
from scipy import stats
x = [-118,-117.8,-117.7,-80,-70]
y = [18.1,18,18,17.8,18]
lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
binx = lon
biny = lat
ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])
Error:
ValueError: bins must be monotonically increasing or decreasing
How to deal with this issue?
My solution
- Use
zipto get boundaries of every grid;
Add condition to make sure the order is correct
for row in np.arange(lon_b.shape[0]-1):
k = 0
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
# Because binned_statistic_2d need bin_* increase monotonically
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
k = 1
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
python scipy interpolation histogram2d
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.
It's difficult to using binned_statistic_2d like this:
from scipy import stats
x = [-118,-117.8,-117.7,-80,-70]
y = [18.1,18,18,17.8,18]
lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
binx = lon
biny = lat
ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])
Error:
ValueError: bins must be monotonically increasing or decreasing
How to deal with this issue?
My solution
- Use
zipto get boundaries of every grid;
Add condition to make sure the order is correct
for row in np.arange(lon_b.shape[0]-1):
k = 0
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
# Because binned_statistic_2d need bin_* increase monotonically
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
k = 1
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
python scipy interpolation histogram2d
I'm using Lambert projection which means if longitude is monotonic then latitude is non-monotonic.
It's difficult to using binned_statistic_2d like this:
from scipy import stats
x = [-118,-117.8,-117.7,-80,-70]
y = [18.1,18,18,17.8,18]
lon = [-118.09100342, -117.987854, -117.88466644, -117.78141785, -117.67811584, -72.32189178, -72.21858215, -72.11533356, -72.012146, -71.90899658]
lat = [17.94124222, 17.96657181, 17.99184036, 18.01698875, 18.04202652,
18.04202652, 18.01698875, 17.99184036, 17.96657181, 17.94124222]
binx = lon
biny = lat
ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx,biny])
Error:
ValueError: bins must be monotonically increasing or decreasing
How to deal with this issue?
My solution
- Use
zipto get boundaries of every grid;
Add condition to make sure the order is correct
for row in np.arange(lon_b.shape[0]-1):
k = 0
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,:]),iter(lat_b[row,:])]*2):
# Because binned_statistic_2d need bin_* increase monotonically
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
k = 1
for lon_1,lat_1,lon_2,lat_2 in zip(*[iter(lon_b[row,1:]),iter(lat_b[row,1:])]*2):
if lat_1 < lat_2:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_1,lat_2],[lon_1,lon_2]]).statistic
else:
CG_bin[row,k] = stats.binned_statistic_2d(lon_CG, lat_CG, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
IC_bin[row,k] = stats.binned_statistic_2d(lon_IC, lat_IC, None,
'count', bins=[[lat_2,lat_1],[lon_1,lon_2]]).statistic
k += 2
python scipy interpolation histogram2d
python scipy interpolation histogram2d
edited Nov 23 at 12:07
asked Nov 22 at 3:11
Xin Zhang
5518
5518
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423331%2fhow-to-use-binned-statistic-2d-with-non-monotonic-boundary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown