Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot average of scattered values in 2D bins as a histogram/hexplot

I have 3 dimensional scattered data x, y, z. I want to plot the average of z in bins of x and y as a hex plot or 2D histogram plot. Is there any matplotlib function to do this? I can only come up with some very cumbersome implementations even though this seems to be a common problem.

E.g. something like this:

enter image description here

Except that the color should depend on the average z values for the (x, y) bin (rather than the number of entries in the (x, y) bin as in the default hexplot/2D histogram functionalities).

like image 704
kuropan Avatar asked Mar 03 '23 03:03

kuropan


1 Answers

If binning is what you are asking, then binned_statistic_2d might work for you. Here's an example:

from scipy.stats import binned_statistic_2d
import numpy as np

x = np.random.uniform(0, 10, 1000)
y = np.random.uniform(10, 20, 1000)
z = np.exp(-(x-3)**2/5 - (y-18)**2/5) + np.random.random(1000)

x_bins = np.linspace(0, 10, 10)
y_bins = np.linspace(10, 20, 10)

ret = binned_statistic_2d(x, y, z, statistic=np.mean, bins=[x_bins, y_bins])

fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 4))
ax0.scatter(x, y, c=z)
ax1.imshow(ret.statistic.T, origin='bottom', extent=(0, 10, 10, 20))

enter image description here

like image 101
Andrea Avatar answered Mar 06 '23 00:03

Andrea