Un modo per farlo è quello di pop il testo creato da annotate
fuori degli assi e aggiungerlo alla figura. In questo modo verrà visualizzato sopra tutte le sottotrame.
Come un rapido esempio del problema che stai avendo:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
plt.show()
Se solo pop l'oggetto di testo fuori degli assi e aggiungerlo alla figura, invece, sarà sulla parte superiore:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=5, ncols=5)
plt.setp(axes.flat, xticks=[], yticks=[], zorder=0)
ax = axes[0,0]
ax.annotate('Testing this out and seeing what happens', xy=(0.5, 0.5),
xytext=(1.1, .5), textcoords='axes fraction', zorder=100)
fig.texts.append(ax.texts.pop())
plt.show()
Lei ha citato il DataCursor
frammento, e non che ci si vuole cambiare il metodo di annotate
:
def annotate(self, ax):
"""Draws and hides the annotation box for the given axis "ax"."""
annotation = ax.annotate(self.template, xy=(0, 0), ha='right',
xytext=self.offsets, textcoords='offset points', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
)
# Put the annotation in the figure instead of the axes so that it will be on
# top of other subplots.
ax.figure.texts.append(ax.texts.pop())
annotation.set_visible(False)
return annotation
Non ho ancora testato l'ultimo bit, ma dovrebbe funzionare ...
Hmmm ... ci dovrebbe essere un modo semplice per farlo, ma non riesco a capirlo ... Se tracciate il testo in figura invece degli assi (ad es. usando 'fig.text'), apparirà sempre sopra. Finché è negli assi, verrà sovrascritto da altre sottotrame, poiché 'zorder' applica solo _inside_ un asse. Posso pensare ad alcuni brutti hack per sostituire tutte le chiamate 'annotate' con' fig.text' ma non è bello ... Ci dovrebbe essere un modo migliore ... –
Incidentalmente, felice che tu abbia trovato utile lo snippet DataCursor ! –