- 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 set the number of ticks in plt.colorbar in Matplotlib?
To set the number of ticks in a colorbar, we can take the following steps−
- Create random data using numpy
- Display the data as an image, i.e., on a 2D regular raster.
- Make a colorbar using colorbar() method with an image scalar mappable object.
- Set the ticks and tick labels of the colorbar using set_ticks() and set_ticklabels() methods.
- 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.rand(4, 4) im = plt.imshow(data, cmap="copper") cbar = plt.colorbar(im) cbar.set_ticks([0.2, 0.4, 0.6, 0.8]) cbar.set_ticklabels(["A", "B", "C", "D"]) plt.show()
Output
- Related Articles
- How to add Matplotlib Colorbar Ticks?
- How to change the color of the ticks in the colorbar in Matplotlib?
- How to set the ticks on a Fixed Position in Matplotlib?
- How can I set the location of minor ticks in Matplotlib?
- How to set axis ticks in multiples of pi in Python Matplotlib?
- How to animate the colorbar in Matplotlib?
- How to decrease colorbar width in Matplotlib?
- How to shift the colorbar position to right in matplotlib?
- How to change the spacing between ticks in Matplotlib?
- How to customize X-axis ticks in Matplotlib?
- How to retrieve colorbar instance from figure in Matplotlib?
- How to add third level of ticks in Python Matplotlib?
- How to disable the minor ticks of a log-plot in Matplotlib?
- How to change the font properties of a Matplotlib colorbar label?
- How to make colorbar orientation horizontal in Python using Matplotlib?

Advertisements