Basj
Basj

Reputation: 46493

Overlay of two layers of different shapes with Matplotlib

I'd like to overlay:

How to imshow them on top of each other? They should both take the full plot width, i.e.:

This doesn't work:

import numpy as np, matplotlib.pyplot as plt, scipy.misc
x = scipy.misc.face(gray=False)     # shape (768, 1024, 3)
y = np.random.random((100, 133))    # shape (100, 133)
fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [5, 1]})
img0 = ax0.imshow(x)  
img1 = ax0.imshow(y, cmap="jet", alpha=0.3)
plt.show()

because a small part of the racoon face is displayed.

For reference, here is the full image:

Upvotes: 1

Views: 796

Answers (1)

Paul Brodersen
Paul Brodersen

Reputation: 13031

You have to match their extent:

enter image description here

import numpy as np
import matplotlib.pyplot as plt

from scipy.misc import face

x = face(gray=False) # shape (768, 1024, 3)
y = np.random.random((100, 133)) # shape (100, 133)
fig, ax = plt.subplots()
img0 = ax.imshow(x)
img1 = ax.imshow(y, cmap="jet", alpha=0.3, extent=img0.get_extent())
plt.show()

Upvotes: 3

Related Questions