1

Is there a method to determine the standard deviation in each bin using the numpy histrogram2d method, given a series of 2d coordinates along with weighted values for each coordinate?

EMal
  • 43
  • 1
  • 4

2 Answers2

1

It's not directly possible with numpy's histrogram2d but with scipy.stats.binned_statistic_2d it can be done quite easily.

from scipy import stats

x = np.random.rand(10000)
y = np.random.rand(10000)
z = np.random.rand(10000)
binx = np.linspace(0,x.max(), 100)
biny = np.linspace(0,y.max(), 100)
hist = stats.binned_statistic_2d(x, y, z, statistic='std', bins=[binx, biny])
plot_x, plot_y = np.meshgrid(binx, biny)

fig, ax = plt.subplots(figsize=(5,5))
pc = ax.pcolormesh(plot_x, plot_y, hist[0].T, cmap="inferno")
ax.set_aspect('equal')
cbar=fig.colorbar(pc,shrink=0.725)
fig.tight_layout()

enter image description here

The statistic option can also take different things like mean, median or a user-defined function, see the documentation for more details.

Jan
  • 405
  • 2
  • 12
  • Hope this can be of help to someone because I just stumbled upon the same problem and found this solution. – Jan Mar 11 '21 at 14:52
  • In addition, one could of course also first use `histogram2d` to derive the bins & co automatically. – Jan Mar 11 '21 at 14:53
0

Not Sure what are you asking, could you post your code?

I generally plot std on hist drawing verticals lines as follows:

plt.axvline(x=np.mean(data)-np.std(data), ls = "--", color='#2ca02c', alpha=0.7)
plt.axvline(x=np.mean(data)+np.std(data), ls = "--", color='#2ca02c', alpha=0.7)

Where "data" is a dictionary with all the valuesIMAGE

CronosVirus00
  • 129
  • 1
  • 1
  • 12
  • I have a series of lat/long points and a measurement for each of these. First I find the sum of these measurements in a grid over a map. I split the map up into say 200 equal bins, using the np.histogram2d method below: sums, _, _ = np.histogram2d(cleanlat, cleanlon, [lat_bins, lon_bins], weights=measurement) With lat/lon_bin indicating my gridding pattern, and cleanlat/lon indicating my measurement points, and measurement as my measurement values. I was wondering if I can find the standard deviation in each bin of the weights, rather than just the sum of the weights – EMal May 19 '17 at 19:02
  • 1
    Try to have a look at this one: the guide shows how to calculate the mean of each bin. Maybe it is possible to do std as well [link] (https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.stats.binned_statistic.html[/link] – CronosVirus00 May 19 '17 at 19:25
  • Thanks very much! I'd been searching for ages for something like that! – EMal May 20 '17 at 15:11