2011-08-12 15 views
33

¿Hay alguna forma de decirle a pyplot.text() una ubicación como puede hacerlo con pyplot.legend()?coloca automáticamente el cuadro de texto en matplotlib

Algo así como el argumento de la leyenda sería excelente:

plt.legend(loc="upper left") 

Estoy tratando de etiquetar subtramas con diferentes ejes usando letras (por ejemplo "A", "B"). Me imagino que debe haber una forma mejor que estimar manualmente la posición.

Gracias

Respuesta

20

No estoy seguro de si esto estaba disponible cuando originalmente publiqué la pregunta, pero ahora se puede usar el parámetro loc. A continuación se muestra un ejemplo:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.offsetbox import AnchoredText 

# make some data 
x = np.arange(10) 
y = x 

# set up figure and axes 
f, ax = plt.subplots(1,1) 

# loc works the same as it does with figures (though best doesn't work) 
# pad=5 will increase the size of padding between the border and text 
# borderpad=5 will increase the distance between the border and the axes 
# frameon=False will remove the box around the text 

anchored_text = AnchoredText("Test", loc=2) 
ax.plot(x,y) 
ax.add_artist(anchored_text) 

plt.show() 

enter image description here

+1

'AnchoredText' no parece manejar' loc = "best" '? – zyxue

+0

@zyxue: ver https://github.com/matplotlib/matplotlib/issues/1313 – naught101

31

Sólo tiene que utilizar annotate y especificar las coordenadas de los ejes. Por ejemplo, "superior izquierda" sería:

plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction') 

Además, puede pedir más elegante y especificar un desplazamiento de puntos constante:

plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top' 
      xycoords='axes fraction', textcoords='offset points') 

Para una explicación más detallada ver los ejemplos here y los ejemplos más detallados here .

+0

@Garrett - Gracias! Deberían arreglarse ahora. –

Cuestiones relacionadas