I want to create an rectangle with a text inside for a gant chart with matplotlib. But if one is zooming in and out the text should never be bigger than the rectangle. So as soon as the text (with a fixed font size) is bigger than the rectangle it should disappear and as sson as it is smaller again (zooming in) it should appear again.
This is an example of a simple text with the problem that the text does not stay inside the rectangle when zooming (is not disappearing as soon as it becoems to big).
from matplotlib import pyplot as plt, patches
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
rectangle = patches.Rectangle((0, 0), 3, 3, edgecolor='orange',
facecolor="green", linewidth=7)
ax.add_patch(rectangle)
rx, ry = rectangle.get_xy()
cx = rx + rectangle.get_width()/2.0
cy = ry + rectangle.get_height()/2.0
ax.annotate("Rectangle", (cx, cy), color='black', weight='bold', fontsize=10, ha='center', va='center')
plt.xlim([-5, 5])
plt.ylim([-5, 5])
plt.show()
Text should disappear now

To monitor the zoom state of your plot, you can connect to the "xlim_changed" and "ylim_changed" events of the
Axesclass. Apart from the zoom state, you might also want to factor in changes of the figure size, which you can monitor via the "resize_event" of the figure canvas.Below, I adjusted your code so that
Alternatively, you might want to make the showing and hiding of your text directly depend on the rectangle's screen size (show text as long as both width and height of the rectangle are greater than the width and height of the text). In this case, you might want to adjust the
on_size_change()function as follows:This might not be perfect, yet, but I hope it gives you an idea on how to proceed.