- 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
Plot a histogram with Y-axis as percentage in Matplotlib
To plot a histogram with Y-axis as percentage in matplotlib, we can take the following steps −
Create a list of numbers as y.
Create a number of bins.
Plot a histogram using hist() method, where y, bins, and edgecolor are passed in the argument.Store the patches to set the percentage on Y-axis.
Create a list of colors from the given alphanumeric numbers.
To set the percentage, iterate the patches (obtained in step 3).
Set the Y-axis ticks range.
To display the figure, use show() method.
Example
import random import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = [4, 1, 8, 7, 6, 4, 2, 1, 2, 5] nbins = 10 _, _, patches = plt.hist(y, bins=nbins, edgecolor='white') colors = ["#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in range(nbins)] for patch in patches: patch.set_facecolor(colors[np.random.randint(100) % nbins]) x = patch.get_x() + patch.get_width() / 2 y = patch.get_height() + .05 plt.annotate('{:.1f}%'.format(y), (x, y), ha='center') plt.ylim(0, 3) plt.show()
Output
- Related Articles
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- Plot a histogram with colors taken from colormap in Matplotlib
- Python - Plot a Histogram for Pandas Dataframe with Matplotlib?
- Show the origin axis (x,y) in Matplotlib plot
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to plot a 2D histogram in Matplotlib?
- How to plot an image with non-linear Y-axis with Matplotlib using imshow?
- How to plot hexbin histogram in Matplotlib?
- How to center labels in a Matplotlib histogram plot?
- How to plot a histogram using Matplotlib in Python with a list of data?
- How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?
- How to plot collections.Counter histogram using Matplotlib?
- Plot two horizontal bar charts sharing the same Y-axis in Python Matplotlib
- How to plot a line graph from histogram data in Matplotlib?
- How to plot multiple Pandas columns on the Y-axis of a line graph (Matplotlib)?

Advertisements