Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Solution 2:

@Andrea's answer is very clear and helpful, but I wanted to mention a faster alternative that does not use the scipy library.

The idea is to do a 2d histogram of x and y weighted by the z variable (it has the sum of the z variable in each bin) and then normalize against the histogram without weights (it has the number of counts in each bin). In this way, you will calculate the average of the z variable in each bin.

The code:

import numpy as np
import matplotlib.pyplot as plt

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

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

H, xedges, yedges = np.histogram2d(x, y, bins = [x_bins, y_bins], weights = z)
H_counts, xedges, yedges = np.histogram2d(x, y, bins = [x_bins, y_bins]) 
H = H/H_counts

plt.imshow(H.T, origin='lower',  cmap='RdBu',
            extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
plt.colorbar()

enter image description here

In my computer, this method is approximately a factor 5 faster than using scipy's binned_statistic_2d.

Post a Comment for "Plot Average Of Scattered Values In 2d Bins As A Histogram/hexplot"