0

can anybody, please, help me to understand the ouput of this code?? I want simply to obtain an histogram with the specified bin edges and bin frequency values.

edges=np.array([106,150,212,300,425,600,850,875])
freq=np.array([0.02,0.15,16.55,41.19,27.18,11.72,1.97])
plt.hist(freq,bins=edges)
plt.show()

2 Answers 2

2

plt.hist(x) computes and then draws a histogram of x. Your data is already in histogram form. Simply plot it with:

mids = 0.5 * (edges[:-1] + edges[1:]) # Midpoints of the histogram bins
plt.bar(mids, freq, 40) # Draw a bar chart with bars of width 40
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

I agree with with @Seb about the data already being in histogram form, but could we still use plt.hist() by passing the edges as both the data and the bins and pass freq as the weights:

import matplotlib.pyplot as plt

edges = [106, 150, 212, 300, 425, 600, 850, 875]
freq = [0.02, 0.15, 16.55, 41.19, 27.18, 11.72, 1.97, 0]

plt.hist(edges, bins=edges, weights=freq)
plt.show()

enter image description here

As @Seb's plt.bar() approach doesn't visually emphasize the width of the buckets:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.