FastCycling
FastCycling

Reputation: 3

Plot an histogram from an array of frequencies ordered by ascending values

I would like to plot an histogram with on the X axes the height and on the Y axis the frequency of this height. My dataset looks like that

[10000,200,3000,5400,...]

the first number is the number of people who are 170cm tall the second 171cm ... I want to make an histogram with the first bin showing how many people are between 170cm and 175cm tall the second 175-180 ...

Upvotes: 0

Views: 1096

Answers (1)

JohanC
JohanC

Reputation: 80289

Usually, a histogram calculates how many of its input array go into each of a set of bins. And then creates a bar plot.

As you already have the frequencies, you can directly create a bar plot with the heights on the x-axis and the frequencies on the y-axis.

To have frequencies per 5, just sum up 5 subsequent height frequencies.

Here is some code to illustrate the concepts:

import matplotlib.pyplot as plt

min_height = 153
max_height = 191
height_freq = [1, 2, 2, 4, 5, 11, 52, 53, 113, 193, 292, 442, 550, 792, 983, 1311, 1553, 1735, 1949, 1974, 2082,
               2031, 1852, 1591, 1416, 1142, 816, 729, 478, 333, 211, 125, 78, 49, 20, 15, 11, 2, 2]

fig, ax = plt.subplots(ncols=2, figsize=(12, 4))

heights = range(min_height, max_height + 1)
ax[0].bar(heights, height_freq, color='crimson', edgecolor='white')
ax[0].set_ylabel('height frequency')

heights_per_5 = range(min_height, max_height + 1, 5)
height_freq_per_5 = [sum(height_freq[h - min_height:h - min_height + 5]) for h in heights_per_5]

ax[1].bar(heights_per_5, height_freq_per_5, width=4.5, align='edge', color='limegreen', edgecolor='white')
ax[1].set_ylabel('height frequency (per 5 cm)')

plt.show()

example plot

Upvotes: 1

Related Questions