- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 colors taken from colormap in Matplotlib
To plot a histogram with colors taken from colormap, we can use the setp() method.
Steps
Create data points using numpy.
Plot data (Step 1) using hist() method, with bins=25, rwidth=.75,...etc.
Returned values n, bins and patches can help to find col.
Get a colormap instance for name "RdYlBu".
Zip the col and patches.
Now, using setp() method, set the property of each patch.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.random(1000) n, bins, patches = plt.hist(data, bins=25, density=True, color='red', rwidth=0.75) col = (n-n.min())/(n.max()-n.min()) cm = plt.cm.get_cmap('RdYlBu') for c, p in zip(col, patches): plt.setp(p, 'facecolor', cm(c)) plt.show()
Output
- Related Articles
- Matplotlib Plot Lines with Colors through Colormap
- Plot a histogram with Y-axis as percentage in Matplotlib
- Python - Plot a Histogram for Pandas Dataframe with Matplotlib?
- How to plot a line graph from histogram data in Matplotlib?
- How to plot data into imshow() with custom colormap in Matplotlib?
- 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 to specify different colors for different bars in a Python matplotlib histogram?
- How to plot a histogram using Matplotlib in Python with a list of data?
- How to plot collections.Counter histogram using Matplotlib?
- Plot a polar color wheel based on a colormap using Python/Matplotlib
- Plot different colors for different categorical levels using matplotlib
- Matplotlib – Make a Frequency histogram from a list with tuple elements in Python
- Plotting a histogram from pre-counted data in Matplotlib

Advertisements