- 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 to add a colorbar for a hist2d plot in Matplotlib?
To add a colorbar for hist2d plot, we can pass a scalar mappable object to colorbar() method's argument.
Steps
Create x and y data points using numpy.
Create a figure and a set of subplots using subplots() method.
Make a 2D histogram plot using hist2d() method.
Create a colorbar for a hist2d scalar mappable instance.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt, colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(100) y = np.random.randn(100) + 5 fig, ax = plt.subplots() hh = ax.hist2d(x, y, bins=40, norm=colors.LogNorm()) fig.colorbar(hh[3], ax=ax) plt.show()
Output
- Related Articles
- How to make a discrete colorbar for a scatter plot in matplotlib?
- How to plot a pcolor colorbar in a different subplot in Matplotlib?
- How to add Matplotlib Colorbar Ticks?
- How to plot a 2D matrix in Python with colorbar Matplotlib?
- How to plot scatter points in a 3D figure with a colorbar in Matplotlib?
- How do I use colorbar with hist2d in matplotlib.pyplot?
- Setting the limits on a colorbar of a contour plot in Matplotlib
- How to add text inside a plot in Matplotlib?
- How to draw a log-normalized imshow plot with a colorbar representing the raw data in Matplotlib?
- How to add vertical lines to a distribution plot (sns.distplot) in Matplotlib?
- How to Add Markers to a Graph Plot in Matplotlib with Python?
- How to Adjust the Position of a Matplotlib Colorbar?
- How to decrease colorbar width in Matplotlib?
- How to animate the colorbar in Matplotlib?
- Add a custom border to certain cells in a Matplotlib / Seaborn plot

Advertisements