Why don't you use imshow instead?

You can plot a 2D image by doing:

plt.imshow(Image1, cmap='gray') # I would add interpolation='none'

Afterwards, you can easily overlay the segmentation by doing:

plt.imshow(Image2_mask, cmap='jet', alpha=0.5) # interpolation='none'

Changing the alpha will change the opacity of the overlay.

Additionaly, why do you create 2 masks? Only one should be enough, you can do:

Image2_mask = ma.masked_array(Image2 > 0, Image2)

Practical example:

import numpy as np
mask = np.zeros((10,10))
mask[3:-3, 3:-3] = 1 # white square in black background
im = mask + np.random.randn(10,10) * 0.01 # random image
masked = np.ma.masked_where(mask == 0, mask)

import matplotlib.pyplot as plt
plt.figure()
plt.subplot(1,2,1)
plt.imshow(im, 'gray', interpolation='none')
plt.subplot(1,2,2)
plt.imshow(im, 'gray', interpolation='none')
plt.imshow(masked, 'jet', interpolation='none', alpha=0.7)
plt.show()

Answer from Imanol Luengo on Stack Overflow
🌐
Matplotlib
matplotlib.org β€Ί stable β€Ί gallery β€Ί images_contours_and_fields β€Ί layer_images.html
Layer images with alpha blending β€” Matplotlib 3.11.2 documentation
import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
🌐
Plotly
community.plotly.com β€Ί πŸ“Š plotly python
How to overlay two images with opacity - πŸ“Š Plotly Python - Plotly Community Forum
June 7, 2023 - I have a simple problem, I want to overlay two rgb images (np.arrays with three channels and same size) over each other, with opacity setting for the top so I can see both. fig_visual_check = go.Figure() fig_visual_check = fig_visual_check.add_trace(go.Image(z=images_merged_visual[0], opacity=0.5)) fig_visual_check = fig_visual_check.add_trace(go.Image(z=images_merged_visual[1], opacity=1)) fig_visual_check However, the result is just a black image.
🌐
Matplotlib
matplotlib.org β€Ί 3.1.1 β€Ί gallery β€Ί images_contours_and_fields β€Ί layer_images.html
Layer Images β€” Matplotlib 3.1.2 documentation
May 1, 2020 - import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 76623437 β€Ί how-to-overlay-merge-tow-images-using-plt-image
matplotlib - How to overlay/ merge tow images using plt.image - Stack Overflow
Copyimport matplotlib.pyplot as plt import numpy as np from PIL import Image cm = plt.cm.RdBu_r cm.set_under('white') # load both images frame1 = np.asarray(Image.open('polarbear.png')) frame2 = np.asarray(Image.open('dog.png')) # overlay both images using an alpha fig, ax = plt.subplots() ax.imshow(frame1, cmap=cm, vmin=12, alpha=0.5) ax.imshow(frame2, alpha=0.5) ax.set_axis_off() # display result #plt.savefig('plot_output.png') plt.show() ... Sign up to request clarification or add additional context in comments. ... the thing I can not maintain the 'white' background as I am plotting two trajectories 2023-07-05T20:58:47.683Z+00:00
🌐
Moonbooks
moonbooks.org β€Ί Articles β€Ί How-to-overlay--superimpose-two-images-using-python-and-pillow-
How to overlay / superimpose two images using python and pillow ?
August 24, 2022 - from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) background.save('how_to_superimpose_two_images_01.png',"PNG")
🌐
CSDN
devpress.csdn.net β€Ί python β€Ί 630452787e66823466199c6f.html
Overlay an image segmentation with numpy and matplotlib_python_Mangs-Python
August 23, 2022 - I am coming from a Matlab environment and I am quite new to Python. ... Changing the alpha will change the opacity of the overlay. Additionaly, why do you create 2 masks? Only one should be enough, you can do: ... import numpy as np mask = np.zeros((10,10)) mask[3:-3, 3:-3] = 1 # white square in black background im = mask + np.random.randn(10,10) * 0.01 # random image masked = np.ma.masked_where(mask == 0, mask) import matplotlib.pyplot as plt plt.figure() plt.subplot(1,2,1) plt.imshow(im, 'gray', interpolation='none') plt.subplot(1,2,2) plt.imshow(im, 'gray', interpolation='none') plt.imshow(masked, 'jet', interpolation='none', alpha=0.7) plt.show()
🌐
Quora
quora.com β€Ί How-can-I-overlay-2-images-of-different-sizes-using-Python
How to overlay 2 images of different sizes using Python - Quora
Answer (1 of 2): overlay of 2 images of different sizes I posted details on github: pydemo/overlay Background image Foreground image Get images width/height [code]b_h, b_w, b_ch = background.shape o_h, o_w, o_ch = overlay.shape [/code]Scale background image [code] W = 800 imgScale =...
Find elsewhere
🌐
GitHub
github.com β€Ί RemiLehe β€Ί transparent_imshow
GitHub - RemiLehe/transparent_imshow: Pixel-dependent transparency with matplotlib's imshow
This is a simple wrapper around matplotlib's imshow function, which allows to produce images with pixel-dependent transparency. This can be particularly useful to overlay several images. The solution implemented was inspired by this post. Below is a simple example that shows how to overlay two arrays of data, by using the function transp_imshow which is provided in this package.
Starred by 3 users
Forked by 2 users
Languages: Python 100.0% | Python 100.0%
🌐
LabEx
labex.io β€Ί tutorials β€Ί overlay-image-on-matplotlib-plot-49029
Overlay Image on Matplotlib Plot | Python Tutorials | LabEx
A value of 0 is completely transparent, and 1 is completely opaque. Run the cell by pressing Shift+Enter. The output should show the same bar chart as before, but now with the Matplotlib logo overlaid at the bottom left corner.
🌐
Finxter
blog.finxter.com β€Ί 5-effective-ways-to-plot-a-layered-image-in-matplotlib-in-python
5 Effective Ways to Plot a Layered Image in Matplotlib in Python – Be on the Right Side of Change
March 6, 2024 - In this snippet, a base layer is created as a grayscale image showing the sine-cosine function values. The contourf() function then overlays this with filled contour lines using a different color map and sets the transparency using the alpha parameter. The add_axes() method permits precise control over the axes of the plots, allowing for the addition of extra layers in exact positions. This approach is helpful when precise alignment of layers is needed. ... import matplotlib.pyplot as plt import numpy as np # Create the base figure fig = plt.figure() # Add first subplot, this will be the base layer ax1 = fig.add_subplot(111) # Add a second subplot as an overlay, position identical to ax1 ax2 = fig.add_axes(ax1.get_position(), frameon=False) # Generate some data x = np.arange(0, 10, 0.1) y1 = np.exp(x) y2 = np.sin(x) ax1.plot(x, y1, 'b-') ax2.plot(x, y2, 'r-', alpha=0.5) plt.show()
🌐
Matplotlib
matplotlib.org β€Ί 2.0.2 β€Ί examples β€Ί pylab_examples β€Ί layer_images.html
pylab_examples example code: layer_images.py β€” Matplotlib 2.0.2 documentation
""" Layer images above one another using alpha blending """ from __future__ import division import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x/2 + x**5 + y**3)*np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
🌐
TutorialsPoint
tutorialspoint.com β€Ί overlay-an-image-segmentation-with-numpy-and-matplotlib
Overlay an image segmentation with Numpy and Matplotlib
Use imshow() method to display data as an image, i.e., on a 2D regular raster. To display the figure, use show() method. from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True mask = np.zeros((10, 10)) ...
🌐
Matplotlib
matplotlib.org β€Ί 3.5.0 β€Ί gallery β€Ί images_contours_and_fields β€Ί layer_images.html
Layer Images β€” Matplotlib 3.5.0 documentation
import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί overlay-an-image-on-another-image-in-python
Overlay an image on another image in Python - GeeksforGeeks
January 3, 2021 - Firstly we opened the primary image and saved its image object into variable img1. Then we opened the image that would be used as an overlay and saved its image object into variable img2.
🌐
Matplotlib
matplotlib.org β€Ί 3.1.0 β€Ί gallery β€Ί images_contours_and_fields β€Ί layer_images.html
Layer Images β€” Matplotlib 3.1.0 documentation
November 12, 2020 - import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
🌐
Stack Exchange
gis.stackexchange.com β€Ί questions β€Ί 443995 β€Ί python-imshow-will-not-overlay-only-display-last-lines-map
matplotlib - Python imshow will not overlay -- only display last line's map - Geographic Information Systems Stack Exchange
October 30, 2022 - The key would be transparency. You need to make one of the images transparent. That could possibly done by setting an alpha value, see this Stackexchange answer: https://stackoverflow.com/questions/10127284/overlay-imshow-plots-in-matplotli...