I want to create a draggable annotation box with Matplotlib. However, my ax
is defined on a SubFigure
object rather than a Figure
.
The following short code shows how the text box cannot be dragged in that case (unless it's located "outside" the plot area):
import matplotlib.pyplot as plt
fig = plt.figure()
subfig = fig.subfigures()
ax = subfig.add_subplot()
bbox_args = dict(boxstyle="round", facecolor="wheat")
an1 = ax.annotate("Text is outside, so draggable", xy=(0.5, 1.1), xycoords=ax.transAxes, bbox=bbox_args)
an2 = ax.annotate("Text is inside, so not draggable", xy=(0.3, 0.5), xycoords=ax.transAxes, bbox=bbox_args)
an1.draggable()
an2.draggable()
plt.show()
In fact, once you drag the outside text box and leave it inside, it will be stuck there forever!
Without relying on subfig
, both boxes would be draggable, but how to do it with subfig
?