Reputation: 1412
I am trying to draw a rectangle on top of a data plot in matplotlib
. To do this, I have this code
import matplotlib.patches as patches
import matplotlib.pyplot as pl
...
fig = pl.figure()
ax=fig.add_axes([0,0,1,1])
ax.add_patch(
patches.Rectangle(
(776820, 5000), # (x,y)
3000, # width
3500, # height
fill=False
)
)
ax.plot(signal)
ax.plot(fit)
...
When I do this, the rectangle is behind the data, however. It doesn't appear to matter if I add the rectnagle before or after plotting the actual data. How can I ensure that the rectangle is the top-most element in the figure?
Upvotes: 6
Views: 10975
Reputation: 2385
The matplotlib.patches.Rectangle allows the keyword argument zorder that is by default 1.0.
Choosing a zorder above one should bring your rectangle to the foreground of the image.
ax.add_patch(
patches.Rectangle(
(776820, 5000), # (x,y)
3000, # width
3500, # height
fill=False,
zorder=2
)
)
Upvotes: 14