- 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 to apply a mask on the matrix in Matplotlib imshow?
To apply a mask on the matrix in matplotlib imshow(), we can use np.ma.masked_where() method with lower and upper limit.
Steps
- Initialize two variables, l and u, to mask the input matrix.
- Create random data of 5×5 dimension.
- Mask the input matrix, lower of l value, and above of u.
- Create a figure and a set of subplots with nrows=1 and ncols=
- Display the data as an image, i.e., on a 2D regular raster, at axes 0 and
- Set the title of the axes, 0 and
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True l = 0.125 u = 0.575 data = np.random.rand(5, 5) data = np.ma.masked_where((l < data) & (data < u), data) fig, axs = plt.subplots(1, 2) axs[0].imshow(data.data) axs[0].set_title("Without Masked") axs[1].imshow(data) axs[1].set_title("With Masked") plt.show()
Output
- Related Articles
- How to add legend to imshow() in Matplotlib?
- Change values on matplotlib imshow() graph axis
- How to show two different colored colormaps in the same imshow Matplotlib?
- How to update matplotlib's imshow() window interactively?
- How to change the scale of imshow in matplotlib without stretching the image?
- Defining a discrete colormap for imshow in Matplotlib
- How to plot data into imshow() with custom colormap in Matplotlib?
- Matplotlib – How to set xticks and yticks with imshow plot?
- Adjusting gridlines and ticks in Matplotlib imshow
- How to draw a log-normalized imshow plot with a colorbar representing the raw data in Matplotlib?
- Plotting an imshow() image in 3d in Matplotlib
- How can a simple bivariate distribution be shown using ‘imshow’ in Matplotlib Python?
- How to apply a 3×3 convolution matrix using imageconvolution() in PHP?
- Set two Matplotlib imshow plots to have the same colormap scale
- How can I plot NaN values as a special color with imshow in Matplotlib?

Advertisements