MrYouMath
MrYouMath

Reputation: 603

Python | Plotting histogram with boundaries and frequencies

I have calculated interval boundaries

import numpy as np

boundaries = np.array([-1.0, -0.5, 0.0, 0.5, 1.0])

and normalized frequencies

normalized_frequencies = np.array([0.10, 0.40, 0.40, 0.10])

My question: How can I plot a histogram with the bar boundaries like boundaries and the y-values as normalized_frequencies?

Upvotes: 0

Views: 1118

Answers (1)

Sheldore
Sheldore

Reputation: 39042

You need a bar chart instead of a histogram I think. The point here is to just use the first four boundary values and then put the width equal to the spacing between the boundaries. In this case, the width is 0.5. The black edgecolor is to differentiate the bars.

import numpy as np

boundaries = np.array([-1.0, -0.5, 0.0, 0.5, 1.0])
normalized_frequencies = np.array([0.10, 0.40, 0.40, 0.10])
width = boundaries[1] - boundaries[0]

plt.bar(boundaries[0:-1], normalized_frequencies, width=width, 
        align='edge', edgecolor='k')

enter image description here

Second alternative is to find the center points of each interval

centers = (boundaries[1:] + boundaries[:-1]) / 2
width = centers[1] - centers[0]

plt.bar(centers, normalized_frequencies, width=width, 
        align='edge', edgecolor='k')

Upvotes: 1

Related Questions