2012-12-12 17 views
5

Ho un grafico in matplotlib con più sottotrame (assi) e voglio annotare i punti all'interno degli assi. Tuttavia, le successive annotazioni di sovrapposizione degli assi dagli assi precedenti (ad es., Annotation on subplot (4,4,1) vanno sotto subplot (4,4,2)). Ho impostato l'annotazione su zord bello e alto, ma senza risultato:/Come evitare che un'annotazione matplotlib venga ritagliata da altri assi

Ho usato una versione modificata di DataCursor per le annotazioni.

Qualsiasi aiuto sarebbe molto apprezzato

Ecco un esempio: enter image description here

+0

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 ... –

+0

Incidentalmente, felice che tu abbia trovato utile lo snippet DataCursor ! –

risposta

5

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() 

enter image description here

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() 

enter image description here

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 ...

+0

Grazie mille, Joe :) Ha funzionato perfettamente! E grazie anche per DataCursor. Lo adoro! –

Problemi correlati