Roma  Karageorgievich Roma Karageorgievich - 2 months ago 5
Python Question

How to properly create hist in matplotlib?

I want to plot a histogram with three columns with heights

. Each column will have a width of 1. So the first column will have height
on the interval
, the second one
on the interval
and so on.

plt.hist([5, 10, 20], bins=range(0,4,1))

But as result I have nothing:
enter image description here

What did I do wrong?


hist computes the number of data samples that lies within a given bin and then displays the resulting frequencies as a bar plot. You don't actually need hist because you already have the frequencies. You simply need bar to display these frequencies as a bar plot. The first input specifies the left edge location of each bar and then we can use the width kwarg to specify the width of each bar.

import matplotlib.pyplot as plt[0, 1, 2], [5, 10, 20], width=1)

enter image description here