How can I graph np.histogram() using px.histogram()?

Hi all,

I have data comprised of start and end times for meetings, and I wanted to create a histogram to count the number of meetings in a bin of time. I have used numpy’s np.histogram() as an elegant solution to binning and creating a histogram out of my data. This takes in an array of data and a number of bins and returns an array with the values of the histogram and an array with the edges of the bins.

Here’s an example of the inputs and outputs (the number of bins is 3 here):

Now, I would like to plot it using the lovely plotly. However, I am having trouble porting it over with px.histogram(). I used the nbins argument on my first attempt, but this gave me automated bins, when I already have the bins from np.histogram(). Not only that, but my histogram values weren’t plotting correctly. I also tried using the inputs in fig.update_traces() from this answer, but once again, my histogram values weren’t plotting correctly.

I’d like to have a nice way to take the binning that np.histogram() has already done for me and plot it with px.histogram, or even in another Plotly histogram plot. I’d appreciate any help I can get.

Thank you so much!

The Plotly Histograms trace takes as input the raw data (rather than binned data) and binning parameters to create the binning in the graph whilst retaining all the underlying info.

If you want to use numpy’s binning you could use a bar plot with the bin counts, positioned at the middle of the bins for instance.