How to use binned_statistic_2d with Non-monotonic boundary?
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 |
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 |
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 '18 at 12:07
Xin Zhang
asked Nov 22 '18 at 3:11
Xin ZhangXin Zhang
64211
64211
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
0
active
oldest
votes
0
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.
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