Skip to content
Advertisement

Reproducing a 2d histogram in Python

I’m working with a very large dataset in Python, so I’m trying to use histograms instead of arrays (the arrays get way too large for saving/loading/mapping). I’m crawling over a bunch of files and pulling information from them, and I would like to then take the information and remake the histograms afterwards. I can do this with a 1D histogram as follows:

JavaScript

nSigmaProtonPico is a 2D array to store the bin edges and the final count for the histogram values. nSigmaProtonHisto is a 1D array for a particular event, and I loop over millions of events. Once the script is done, it will have crawled over all the events and I’ll have a 2D array with the histogram values and positions. I can simply graph it, like so:

JavaScript

enter image description here

When I try to do this for a 2D histogram, it falls apart. I’m missing something. Here’s what I have:

JavaScript

This gets me something, but I can’t figure out how to plot it so that I reproduce the histogram I would have from all the data. I would think it would be as simple as x, y, and z coordinates, but there are 4 and not 3 coordinates.

What am I missing?

Advertisement

Answer

counter is a 2D array. Provided you have the same bins at each call of histogram2d, you will get an array of the same size. You can therefore simply add all the counter arrays. Consider:

JavaScript

H1 and H2 are both shape (99,99) (100 edges in each dimension).

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement