2013-02-27 16 views
8

per ogni etichetta segno di spunta sull'asse y tick, vorrei cambiare: label -> 2^labelmatplotlib: cambiamento asseY etichette

sto tramando log-log dei dati (base 2), ma vorrei le etichette per mostrare i valori dei dati originali.

so di poter ottenere gli attuali etichette y con ylabels = plt.getp(plt.gca(), 'yticklabels')

Questo mi dà un elenco: <a list of 9 Text yticklabel objects> ognuno dei quali è un <matplotlib.text.Text object at 0x...>

Ho guardato la documentazione degli oggetti di testo a http://matplotlib.org/users/text_props.html ma non sono ancora sicuro di quale sia la sintassi corretta per cambiare la stringa in ogni etichetta di testo.

volta a cambiare le etichette, ho potuto impostare sull'asse con:

plt.setp(plt.gca(), 'yticklabels', ylabels)

+0

Hai usato qualcosa come 'loglog (x, y, baseX = 2, Basey = 2) '? Quando lo faccio, le etichette sono già nella forma 2^k. –

+0

@WarrenWeckesser no, è in realtà su un diagramma di casella – Joe

risposta

8

Se si vuole fare questo in un caso generale è possibile utilizzare FuncFormatter (vedi: matplotlib axis label format, imshow: labels as any arbitrary function of the image indices. Matplotlib set_major_formatter AttributeError)

Nel caso in cui la si segue dovrebbe funzionare:

import matplotlib as mpl 
import matplotlib.pyplot as plt 

def mjrFormatter(x, pos): 
    return "$2^{{{0}}}$".format(x) 

def mjrFormatter_no_TeX(x, pos): 
    return "2^{0}".format(x) 

ax = plt.gca() 
ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter)) 
plt.draw() 

Il absured {} Escaping è una conseguenza della stringa nuovo stile di frommating

+0

Eseguendo ciò, ricevo avvertenze che non so come interpretare: – Joe

+0

/usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1242: UserWarning: findfont: Font family ['STIXSizeOneSym'] non trovato. Ritornando a Bitstream Vera Sans (prop.get_family(), self.defaultFamily [fontext])) – Joe

+0

/usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1252: UserWarning: findfont: impossibile match: family = Bitstream Vera Sans: style = normal: variant = normal: weight = normal: stretch = normal: size = 12. Restituzione di /usr/share/fonts/thai-scalable/Waree-Oblique.ttf UserWarning) /usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1242: UserWarning: findfont: Font family [' STIXSizeThreeSym '] non trovato. Tornando a Bitstream Vera Sans (prop.get_family(), self.defaultFamily [fontext])) – Joe

Problemi correlati