LegendWK
LegendWK

Reputation: 194

Layer image on top of plot

I'm trying to plot different colors in nine rectangles on top of an image. The easiest solution I've come up with is to plot the rectangles and then layer a transparent background image over it. However when I try to do this the image always appears behind the triangles.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

image_link = r'image.png'
dx = 301
dy = 225
xy0 = (68, 33)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(Rectangle(xy0 , dx, dy)) 
im = plt.imread(image_link)
implot = plt.imshow(im)
plt.show()

I figured since I'm adding the image AFTER my rectangle it'd be shown on top, but no.

Upvotes: 0

Views: 218

Answers (1)

Joao_PS
Joao_PS

Reputation: 670

New code:

image_link = r'image.png'
dx = 301
dy = 225
xy0 = (68, 33)

fig = plt.figure()

ax1 = fig.add_subplot(121)
ax1.add_patch(Rectangle(xy0 , dx, dy)) 
im1 = plt.imread(image_link)
implot1 = plt.imshow(im1, alpha=0.75, zorder=1)

ax2 = fig.add_subplot(122)
ax2.add_patch(Rectangle(xy0 , dx, dy)) 
im2 = plt.imread(image_link)
implot2 = plt.imshow(im2, alpha=1.0, zorder=1)

plt.show()

enter image description here

Upvotes: 1

Related Questions