- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I plot a histogram such that the heights of the bars sum to 1 in matplotlib?
In plt.hist() method, stacked=True could help to get the heights of the bars sum to 1.
Steps
Create a list of numbers.
Using plt.hist(), we can draw the histogram.
stacked : bool, default: False
If "True", multiple data are stacked on top of each other If ``False`` multiple data are arranged side by side if histtype is 'bar' or on top of each other if histtype is 'step'.
density : bool, default: False
If "True", draw and return a probability density: each bin will display the bin's raw count divided by the total number of counts *and the bin width*.
To show the figure, use the plt.show() method.
Example
from matplotlib import pyplot as plt x = [1, 4, 16, 64, 256] # stacked and density are true then the sum of the histograms is normalized to 1. plt.hist(x, 10, stacked=True, density=True) plt.show()
Output
- Related Articles
- How to plot a 2D histogram in Matplotlib?
- How to plot hexbin histogram in Matplotlib?
- How to center labels in a Matplotlib histogram plot?
- How do I plot hatched bars using Pandas and Matplotlib?
- How to plot collections.Counter histogram using Matplotlib?
- How to create histogram like plot with different color of bars in R?
- How to specify different colors for different bars in a Python matplotlib histogram?
- How to write text above the bars on a bar plot (Python Matplotlib)?
- How to plot a line graph from histogram data in Matplotlib?
- How can I add textures to my bars and wedges in Matplotlib?
- How can I plot a confusion matrix in matplotlib?
- How to sort bars in a bar plot in ascending order (Matplotlib)?
- How to plot a histogram using Matplotlib in Python with a list of data?
- How can I get the output of a Matplotlib plot as an SVG?
- How can I plot hysteresis threshold in Matplotlib?

Advertisements