I am trying to change the font that python uses for graphing. I have greek letters and other symbols that I need to use so I have been trying to use their latex form. I would like to save the figure in pdf form. I am using the Spyder environment to program in; my Spyder and python are from anaconda.
I am trying to execute the program specified by http://matplotlib.org/1.3.1/users/usetex.html -- I have followed the instructions and made sure that I have the three prerequisites for this to work and that they are in my path -- "a working LaTeX installation, dvipng (which may be included with your LaTeX installation), and Ghostscript (GPL Ghostscript 8.60 or later is recommended). The executables for these external dependencies must all be located on your PATH."
import numpy as np
import matplotlib.pyplot as plt
# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)
plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)
#plt.savefig('tex_demo')
plt.show()
When I run this program with the second to last line of that program commented out, a graph shows up with the curve plotted, but nothing else - no axis labels or title or axis tick marks.
And if I uncomment the line plt.savefig('tex_demo') or try plt.savefig('test_demo.pdf',format='pdf',bbox_inches='tight') I get the error:
In [3]: runfile('/Users/Emily/Documents/Research/Masters_Thesis/Examples/tex_demo.py', wdir='/Users/Emily/Documents/Research/Masters_Thesis/Examples')
Traceback (most recent call last):
File "<ipython-input-3-9b7e3d9c6b78>", line 1, in <module>
... lots of lines here
File "/Users/Emily/anaconda/lib/python2.7/site-packages/matplotlib/dviread.py", line 876, in find_tex_file
stderr=subprocess.PIPE)
File "/Users/Emily/anaconda/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/Users/Emily/anaconda/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I have tried other variations as well --
import pylab as pl
import matplotlib as mp
...
mp.rc('text', usetex=True)
mp.rc('font', family='serif')
...
get the same results when pl.savefig('tex_demo') is commented (no axes) or uncommented (OS Error).
I looked around to see if anyone else has had the same problem but often the errors associated with "OSError: [Errno 2] No such file or directory" python are not in the same context as I am.
With this variation (Matplotlib not using latex font while text.usetex==True),
from matplotlib import rc
import matplotlib.pylab as plt
rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex=True)
x = plt.linspace(0,5)
plt.plot(x,plt.sin(x))
plt.ylabel(r"This is $\sin(x)$", size=20)
plt.show()
I'll get the error (which is the error I started with but have gotten less frequently after I installed Ghostscript):
Traceback (most recent call last):
... lots of lines here.
File "/Users/Emily/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 670, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "/Users/Emily/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 417, in make_dvi
report))
RuntimeError: LaTeX was not able to process the following string:
'lp'
Here is the full report generated by LaTeX:
with this error, I wasn't sure how to implement the suggestions of TeX in matplotlib on Mac OS X and TeX Live and https://www.pythonanywhere.com/forums/topic/228/
Overall, I would just like to change the font of python while using greek letters or latex characters and save that figure in pdf form. Can anyone spot my error or point me in the right direction to fix my errors?