I am trying to draw a rectangle on my figure such that it overflows on both ends and this overflow is visible.
Example
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize = [5, 5], dpi = 100)
a = plt.Rectangle(tuple((0.4, -0.2)), 0.1, 1.2, color = 'black', alpha=0.8)
ax.add_patch(a)
plt.show(block=True)
fig.tight_layout()
The previous code snippet produces
but I want the rectangle to overflow
Things I have tried
I have tried using zorder
but that doesn't seem to be the solution. I have tried searching online and Stackoverflow but I have not found a similar problem.
Note (Restriction)
I know I could just draw on my plot explicitly using Paint, Powerpoint or similar, but I am restricted in doing that since I am performing a simulation (which produces a gif), so I need to do this natively in Matpotlib.
CodePudding user response:
You can just add clip_on=False
:
a = plt.Rectangle(tuple((0.4, -0.1)), 0.1, 1.2, color = 'black', alpha=0.8, clip_on=False)
Note the slightly changed rectangle values to align the bar.
From matplotlib.artist.Artist.set_clip_on as a brief explanation:
Set whether the artist uses clipping.
When False artists will be visible outside of the Axes which can lead to unexpected results.