Matplotlib Histograms
Matplotlib Histograms
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
y /
Python Delete Files
ADVERTISEMENT
Python Modules
NumPy Tutorial
Pandas Tutorial
SciPy Tutorial
Django Tutorial
Matplotlib Histograms
❮ Previous Next ❯
Python Matplotlib
Matplotlib Intro
Matplotlib Get Started
Matplotlib Pyplot Histogram
Matplotlib Plotting
Matplotlib Markers A histogram is a graph showing frequency distributions.
Matplotlib Line
It is a graph showing the number of observations within each given interval.
Matplotlib Labels
Matplotlib Grid Example: Say you ask for the height of 250 people, you might end up with a histogram like this:
Matplotlib Subplot
Matplotlib Scatter
Matplotlib Bars
Matplotlib Histograms
Matplotlib Pie Charts
Machine Learning
Getting Started
Mean Median Mode
You can read from the histogram that there are approximately:
COLOR PICKER
2 people from 140 to 145cm
5 people from 145 to 150cm
15 people from 151 to 156cm
31 people from 157 to 162cm
46 people from 163 to 168cm
53 people from 168 to 173cm
45 people from 173 to 178cm
28 people from 179 to 184cm
21 people from 185 to 190cm
4 people from 190 to 195cm
Create Histogram
In Matplotlib, we use the hist() function to create histograms.
The hist() function will use an array of numbers to create a histogram, the array is sent into the function as an argument.
For simplicity we use NumPy to randomly generate an array with 250 values, where the values will concentrate around 170, and
the standard deviation is 10. Learn more about Normal Data Distribution in our Machine Learning Tutorial.
import numpy as np
print(x)
Result:
This will generate a random result, and could look like this:
ADVERTISEMENT
Try it Yourself »
The hist() function will read the array and produce a histogram:
Example
A simple histogram:
plt.hist(x)
plt.show()
Result:
Try it Yourself »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.