Al momento se ho impostato matplotlib y asse ticklabels alla modalità scientifica mi dà un esponente in cima dell'asse y della forma 1e-5
Regolare il testo esponente dopo aver impostato i limiti scientifici su matplotlib asse
mi piacerebbe per regolare questo per leggere r'$\mathregular{10^{-5}}$'
in modo che venga stampato correttamente.
Ecco il mio codice di esempio:
# Create a figure and axis
fig, ax = plt.subplots()
# Plot 100 random points
# the y values of which are very small
ax.scatter(np.random.rand(100), np.random.rand(100)/100000.0)
# Set the y limits appropriately
ax.set_ylim(0, 1/100000.0)
# Change the y ticklabel format to scientific format
ax.ticklabel_format(axis='y', style='sci', scilimits=(-2, 2))
# Get the offset value
offset = ax.yaxis.get_offset_text()
# Print it out
print '1st offset printout: {}'.format(offset)
# Run plt.tight_layout()
plt.tight_layout()
# Print out offset again - you can see the value now!
print '2nd offset printout: {}'.format(offset)
# Change it to latex format
offset.set_text(r'$\mathregular{10^{-5}}$')
# Print it out
print '3rd offset printout: {}'.format(offset)
# Add some text to the middle of the figure just to
# check that it isn't the latex format that's the problem
ax.text(0.5, 0.5/100000.0, r'$\mathregular{10^{-2}}$')
# And show the figure
plt.show()
La mia uscita è simile al seguente:
1st offset printout: Text(0,0.5,u'')
2nd offset printout: Text(0,636.933,u'1e\u22125')
3rd offset printout: Text(0,636.933,u'$\\mathregular{10^{-5}}$')
È possibile trovare il codice e l'output figura here.
Ci sono due stranezze: una è che non posso sovrascrivere il 1e-5 in cima all'asse y (che è l'obiettivo), e l'altro è che devo eseguire plt.tight_layout()
per poter vedere quel valore unicode come offset.
Qualcuno può dirmi dove sto andando male?
Grazie
EDIT: La domanda iniziale non ha chiarito che vorrei rilevare automaticamente l'esponente come è attualmente calcolato da ticklabel_format
. Quindi, invece di passare una stringa impostata al testo offset, dovrebbe rilevare automaticamente quel valore e regolare di conseguenza la stringa in lattice.
Molto pulito. Presumo che questo sia un bug in matplotlib dato che dovresti essere in grado di sostituire direttamente la stringa 'offsettext'. La tua descrizione è un po 'confusa - non aggiungi quella stringa ma disattiva 'offsettext' e inserisci l'etichetta in lattice nello stesso posto. Idealmente il testo sostitutivo sarebbe basato sulla posizione di ax_axis.get_offset_text(). Get_position() '. Non sono riuscito a trovare un modo per farlo in quanto 'pos' è in unità pixel (credo), vero? –
Hai ragione - modificherai il mio riferimento a 'pos' nella risposta ora. Non riuscivo a capire cosa fare con quei numeri (in effetti - non mi è venuto in mente che fossero coordinate pixel - buona congettura!). Avete suggerimenti su come ottenere i valori da aggiornare senza fare plt.tight_layout()? – KirstieJane