- 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 create a matplotlib colormap that treats one value specially?
To create a matplotlib colormap that treats one value specially, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Get a colormap instance, name is "rainbow".
- Set the color for low out-of-range values, using set_under('red') method.
- Create random data and eps using numpy.
- Create a figure and a set of subplots.
- Display data as an image, i.e., on a 2D regular raster, using imshow() method.
- Create a colorbar for a ScalarMappable instance, im.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True cmap = plt.get_cmap('rainbow') cmap.set_under('red') data = np.random.randn(5, 5) eps = np.spacing(0.0) fig, ax = plt.subplots() im = ax.imshow(data, interpolation='nearest', vmin=eps, cmap=cmap) fig.colorbar(im, extend='min') plt.show()
Output
- Related Articles
- How to display the matrix value and colormap in Matplotlib?
- How to redefine a color for a specific value in a Matplotlib colormap?
- How to extract a subset of a colormap as a new colormap in Matplotlib?
- How to set a default colormap in Matplotlib?
- How to make the Parula colormap in Matplotlib?
- How to draw node colormap in NetworkX/Matplotlib?
- How to increase colormap/linewidth quality in streamplot Matplotlib?
- Add alpha to an existing Matplotlib colormap
- How to shade points in a scatter based on colormap in Matplotlib?
- Defining the midpoint of a colormap in Matplotlib
- Defining a discrete colormap for imshow in Matplotlib
- Set a colormap of an image in Matplotlib
- Extract Matplotlib colormap in hex-format
- How to plot data into imshow() with custom colormap in Matplotlib?
- Matplotlib Plot Lines with Colors through Colormap

Advertisements