2

I am new to python and am trying to plot a rolling 2D histogram. I've looked at a bunch of other answers and have come up blank. Here is the problem: I have a series of X, Y, Z points - about 1,500 of them. That when you plot X-Y looks as follows:

data

I then want to bin and plot the data according to the Z-value. Here is the core of the code I use:

zi, yi, xi = np.histogram2d(y, x, bins=(bin_size, bin_size), weights=z, normed=False)
counts, _, _ = np.histogram2d(y, x, bins=(bin_size,bin_size))
zi = zi / counts

fig, ax = plt.subplots()
ax.pcolormesh(xi, yi, zi, cmap='coolwarm')

And this is the result:

data

The data is quite coarse. So what I would like to do is to create a 'rolling average' 2D histogram. I would like to create a rolling average of Z, with a “window” size of 4 inch x 4 inch, in steps of 0.05 feet. Note the X-Y axis are in increments of 1 foot.

So in other words every 0.05 feet (6 inches) I want to draw a 4x4 inch box around this point and measure the number of points. I them want to plot that according to the mean-Z for that 4x4 box and then shift over 0.05 feet.

The result should look something like the below:

data

Any help gratefully received

JohnB
  • 67
  • 2
  • 13
  • Are you looking for 2D moving average? https://stackoverflow.com/questions/23000260/numpy-two-dimensional-moving-average – MB-F May 07 '18 at 08:55
  • Yup - that could work. Can I use 'size' to measure distance? – JohnB May 07 '18 at 09:17
  • Sorry @kazemakase - I can't seem to get that work. When I call uniform_filter it returns the same pattern – JohnB May 07 '18 at 13:49
  • did you apply it to the raw data? it may work if applied to the histogram `zi`. – MB-F May 07 '18 at 13:52
  • I applied it to zi `mean_data = ndimage.uniform_filter(zi, size=0.3, mode='constant')` -- size > 1 gives an error; size <= 1 gives same as above. – JohnB May 07 '18 at 17:12
  • Got it working now. Issue was a stray nan. Thanks @kazemakase – JohnB May 08 '18 at 04:34
  • You could use kernel density estimation – jtlz2 Jan 23 '23 at 15:14

0 Answers0