2010-03-29 3 views
18

Il seguente script:Sans-serif matematica con il lattice in matplotlib

import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as mpl 

mpl.rc('font', family='sans-serif') 
mpl.rc('text', usetex=True) 

fig = mpl.figure() 
ax = fig.add_subplot(1,1,1) 
ax.text(0.2,0.5,r"Math font: $451^\circ$") 
ax.text(0.2,0.7,r"Normal font (except for degree symbol): 451$^\circ$") 

fig.savefig('test.png') 

è un tentativo di utilizzare un tipo di carattere sans-serif in matplotlib con LaTeX. Il problema è che il font matematico è ancora un font serif (come indicato dai numeri degli assi e come dimostrato dalle etichette al centro). C'è un modo per impostare il font matematico per essere anche sans-serif?

+3

' "$ \ mathsf {451^\ circ} $" r'? – kennytm

+0

Vedere http://stackoverflow.com/questions/17958485/matplotlib-not-using-latex-font-while-tex-usetex-true/17967324?noredirect=1#17967324 – Dirklinux

+0

la mia risposta di seguito funziona? –

risposta

19

Ho sempre text.usetex = True nel mio file matplotlibrc. In aggiunta a ciò, io uso anche questo:

mpl.rcParams['text.latex.preamble'] = [ 
     r'\usepackage{siunitx}', # i need upright \micro symbols, but you need... 
     r'\sisetup{detect-all}', # ...this to force siunitx to actually use your fonts 
     r'\usepackage{helvet}', # set the normal font here 
     r'\usepackage{sansmath}', # load up the sansmath so that math -> helvet 
     r'\sansmath'    # <- tricky! -- gotta actually tell tex to use! 
] 

Sperare che aiuti.

+0

Questo è esattamente ciò di cui avevo bisogno per ottenere il carattere helvetica (parola chiave :) che funziona con matplotlib/pylab nelle mie trame. E ho provato un bel po 'di cose! Spero che google indicizzerà questo in caso contrario altri tenteranno di fare lo stesso. – SullX

+1

Perfetto. Per ulteriori opzioni sui font, consulta http://www.tug.dk/FontCatalogue/sansseriffonts.html e sostituisci "\ usepackage {helvet}" con il tuo preferito :) – np8

12

Il modo più semplice è quello di utilizzare TeX di matplotlib interna, ad esempio:

import pylab as plt 
params = {'text.usetex': False, 'mathtext.fontset': 'stixsans'} 
plt.rcParams.update(params) 

Se si utilizza un LaTeX esterna, è possibile utilizzare, ad esempio, CM caratteri luminosi:

params = {'text.usetex': True, 
      'text.latex.preamble': [r'\usepackage{cmbright}', r'\usepackage{amsmath}']} 
plt.rcParams.update(params) 

nota, che il carattere CM Bright non è scalabile e non potrai salvare PDF/PS! Lo stesso con altre opzioni con LaTeX esterno che ho trovato finora.

+0

Come posso rimuovere la forma corsiva usando il TeX interno di matplotlib? – Ger

1

Questo vi permetterà di utilizzare il tipo di carattere Computer Modern Sans se, come me, preferisce sopra Helvetica:

mpl.rcParams['text.usetex'] = True 
mpl.rcParams['text.latex.preamble'] = [r'\usepackage[cm]{sfmath}'] 
mpl.rcParams['font.family'] = 'sans-serif' 
mpl.rcParams['font.sans-serif'] = 'cm' 
+0

Questo ha funzionato per me, e avevo faticato a trovare ciò che i caratteri moderni del computer venivano chiamati nel back-end. Non sono riuscito a trovare una lista da nessuna parte. – user3087409