- 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
How can I plot NaN values as a special color with imshow in Matplotlib?
First, we can create an array matrix with some np.nan value, and using imshow method, we can create a diagram for that matrix.
Steps
Create a new figure, or activate an existing figure.
Add an `~.axes.Axes` to the figure as part of a subplot arrangement, nrows = 1, ncols = 1, index = 1.
Create a 2D array with np.nan.
Display data as an image, i.e., on a 2D regular raster.
Use the draw() method which draws the drawing at the given location.
To show the figure, use the plt.show() method.
Example
import numpy as np import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot(111) a = [ [1, 3, 5, np.nan, 8, 9, np.nan], [11, 13, 51, 71, 18, 19, 10], [11, 31, 51, 71, 81, 91, 10], [10, 30, 50, 70, np.nan, np.nan, np.nan], [np.nan, 3, 5, np.nan, 8, 9, np.nan] ] ax.imshow(a, interpolation='nearest', vmin=0, vmax=24) f.canvas.draw() plt.show()
Output
- Related Articles
- How to plot and work with NaN values in Matplotlib?
- How to plot masked and NaN values in Matplotlib?
- Matplotlib – How to set xticks and yticks with imshow plot?
- How to plot data into imshow() with custom colormap in Matplotlib?
- How can I get the output of a Matplotlib plot as an SVG?
- How can I plot a confusion matrix in matplotlib?
- How to plot an image with non-linear Y-axis with Matplotlib using imshow?
- How can I plot hysteresis threshold in Matplotlib?
- How can I plot a single point in Matplotlib Python?
- How to draw a log-normalized imshow plot with a colorbar representing the raw data in Matplotlib?
- How can I place a table on a plot in Matplotlib?
- How can I convert numbers to a color scale in Matplotlib?
- How to plot a gradient color line in matplotlib?
- Change values on matplotlib imshow() graph axis
- How to deal with NaN values while plotting a boxplot using Python Matplotlib?

Advertisements