Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy histogram with 3 variables

Please forgive me if this is a repeated question, I've done my best to look for a solution. This seems very straightforward but I can't seem to find anything applicable.

I'm trying to generate a plot (like a heatmap) using data from 3 1-D numpy arrays. The data is basically arranged as follows:

x_axis = ([1, 4, 6])
y_axis = ([2, 5, 7])
z_axis = ([5, 8, 9])

(my data sets are actually much larger... sometimes hundreds of thousands of entries).

so I've got z_axis values that are each associated with an x-coordinate and y-coordinate... for example, the point (1,2) has the value 5 associated with it.

All I want to do is plot this in such a way that the z values are averaged out for whatever bin size I specify, and color-coded like a heatmap. So, for instance, if I've got 10 data points that fall within a given bin, their z-values will be averaged and that value will fall somewhere on a color spectrum.

Thanks for any help you can provide.

like image 732
Teachey Avatar asked Jul 08 '13 14:07

Teachey


People also ask

How do you make a histogram with multiple variables in Python?

plt. hist() method is used multiple times to create a figure of three overlapping histograms. we adjust opacity, color, and number of bins as needed. Three different columns from the data frame are taken as data for the histograms.

How do you plot a 3D histogram in Python?

Use bar3d() method to plot 3D bars. To hide the axes use axis('off') class by name. To display the figure, use show() method.

How do you plot a histogram using Numpy in Python?

Creating Numpy HistogramNumpy has a built-in numpy. histogram() function which represents the frequency of data distribution in the graphical form. The rectangles having equal horizontal size corresponds to class interval called bin and variable height corresponding to the frequency.

How do you plot an array of a histogram?

plt() Matplotlib can convert this numeric representation of histogram into a graph. The plt() function of pyplot submodule takes the array containing the data and bin array as parameters and converts into a histogram.


1 Answers

From np.histogram2d:

import matplotlib.pyplot as plt
H, xedges, yedges =np.histogram2d(x_axis, y_axis, bins=10, weights=z_axis) 
extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
plt.imshow(H, extent=extent, interpolation='nearest')
plt.colorbar()
plt.show()

Bin count is easily changed.

As Jamie pointed out in the comments if you want the average of the points in each bin:

numbins=10
H, xedges, yedges =np.histogram2d(x_axis, y_axis, bins=numbins, weights=z_axis) 
count, x, y =np.histogram2d(x_axis, y_axis, bins=numbins) 
H/=count
like image 126
Daniel Avatar answered Oct 08 '22 22:10

Daniel