Kenenbek Arzymatov
Kenenbek Arzymatov

Reputation: 9139

How to properly create hist in matplotlib?

I want to plot a histogram with three columns with heights 5, 10 and 20. Each column will have a width of 1. So the first column will have height 5 on the interval [0,1], the second one 10 on the interval [1,2] and so on.

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

But as result I have nothing: enter image description here

What did I do wrong?

Upvotes: 1

Views: 103

Answers (1)

Suever
Suever

Reputation: 65460

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

plt.bar([0, 1, 2], [5, 10, 20], width=1)

enter image description here

Upvotes: 2

Related Questions