- 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
Setting Transparency Based on Pixel Values in Matplotlib
To set transparency based on pixel values in matplotlib, get masked data wherever data is less than certain values. Lesser value will result in full overlapping between two images.
Steps
Create data1 and data2 using numpy.
Get the masked data using numpy's masked_where() method.
Using subplots() method, create a figure and a set of subplots (fig and ax).
Display the data (data1 and masked data) as an image, i.e., on a 2D regular raster, using imshow() method, with different colormaps, jet and gray.
To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data1 = np.random.rand(50, 50) data2 = np.random.rand(50, 50) masked_data = np.ma.masked_where(data2 < .7, data2) fig, ax = plt.subplots() ax.imshow(data2, cmap=cm.gray) ax.imshow(masked_data, cmap=cm.jet, interpolation='none') plt.show()
Output
- Related Articles
- Adjusting Text background transparency in Matplotlib
- Transparency for Poly3DCollection plot in Matplotlib
- Setting the limits on a colorbar of a contour plot in Matplotlib
- Setting Different Bar color in Matplotlib
- How to adjust transparency (alpha) in Seaborn pairplot using Matplotlib?
- Matplotlib Plots Lose Transparency When Saving as .ps/.eps
- Setting Y-axis in Matplotlib using Pandas
- How to get pixel coordinates for Matplotlib-generated scatterplot?
- How to change the transparency/opaqueness of a Matplotlib Table?
- Plot a multicolored line based on a condition in Python Matplotlib
- Setting active subplot using axes object in Matplotlib
- Setting a relative frequency in a Matplotlib histogram
- How to turn off transparency in Matplotlib's 3D Scatter plot?
- How to shade points in a scatter based on colormap in Matplotlib?
- Replace numerical column values based on character column values in R data frame.

Advertisements