Python Bar Graphs: Manual Normalization of Counts and Re-Printing as a Bar Graph

I tried to find something similar, and the closest I could find was this , which helped me extract and process the data, but now I can’t figure out how to rebuild the histogram. I have some array of stresses, and I first built a histogram of the appearance of these stresses. Instead, I want to make a histogram of events per hour (so that the y axis of the normal histogram is divided by the number of hours that I took the data), and then rebuild the histogram using y data.

I have an array that contains the number of events per hour (consisting of the original y axis from pyplot.hist divided by the number of hours the data was received) and the bins from the histogram. I compiled this array using the following code (taken from the answer linked above):

 import numpy import matplotlib.pyplot as pyplot mydata = numpy.random.normal(-15, 1, 500) # this seems to have to be 'uneven' on either side of 0, otherwise the code looks fine. FYI, my actual data is all positive pyplot.figure(1) hist1 = pyplot.hist(mydata, bins=50, alpha=0.5, label='set 1', color='red') hist1_flux = [hist1[0]/5.0, 0.5*(hist1[1][1:]+hist1[1][:-1])] pyplot.figure(2) pyplot.bar(hist1_flux[1], hist1_flux[0]) 

This code does not exactly match what is happening in my code; my data consists of 1000 arrays of 1000 data points each (voltage). I made histograms of this, which gives me the number of occurrences of a given voltage range (or bin width). All I want to do is rebuild the histogram of the number of events per hour (so yaxis histogram / 5 hours) with the same original bin width, but when I divide hist1[0]/5 and repeat it like this, the "bin width" is wrong .

I feel that there should be an easier way to do this, instead of manually rewriting my own histograms.

Thanks in advance, and I'm really sorry if I missed something obvious.

The problem illustrated by the output of my sample code AND my raw data:

Top charts : code snippet output.
Lower charts . My actual data. Upper plots: code snippet output. Lower plots: My actual data.

+6
source share
1 answer

This is because the bar function takes a width argument, which is 0.8 by default ( plt.bar(left, height, width=0.8, bottom=None, hold=None, **kwargs) ), so you need to change it to the distance between the two columns:

 pyplot.bar(hist1_flux[1], hist1_flux[0], width=hist1_flux[1][1] - hist1_flux[1][0]) 
+3
source

All Articles