问题
I'm currently using python 2.7.11
running my code in command prompt
. I am using matplotlib
to create 3D figures for my LaTeX
document. However when I try to use LaTeX
to render my text for the images I receive a windows error message telling me python.exe
has stopped working and I must close the program. Why is this happening and how can I fix it? I'm not tec savvy, so simple answers would be appreciated. Thank you in advance.
Code
The minimum code for this error is:
from mpl_toolkits.mplot3d import Axes3D
import numpy, matplotlib, matplotlib.pyplot as pyplot
matplotlib.rcParams['text.usetex'] = True
Module_Colour = '#F0AE1E'
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
X_arr = numpy.array([1.0,0.0,0.0])
Y_arr = numpy.array([0.0,1.0,0.0])
Z_arr = numpy.array([0.0,0.0,1.0])
O_arr = numpy.array([0.0,0.0,0.0])
pyplot.quiver(O_arr,O_arr,O_arr,X_arr,Y_arr,Z_arr,
pivot='tail', length=1.0, linewidth=2.5,
color = Module_Colour)
pyplot.savefig('C:/Users/alexd/Documents/University/Physics/Physics Figures/fig.jpeg', bbox_inches='tight')
pyplot.show()
回答1:
Note that pyplot.show()
blocks until you close the window.
If you are generating plots for your LaTeX documents, just remove pyplot.show()
.
Make sure that Python can actually find latex
, dvipng
and gs
. (I would expect you to get a error message if that is not the case, but I haven't used python/matplotlib on ms-windows, so I'm not sure.) That is, their location should be in your PATH
environment variable. See the matplotlib documentation on environment variables.
Try the following in an interactive Python session:
>>> import subprocess
>>> subprocess.call(['latex', '--version'])
pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016)
kpathsea version 6.2.2
Copyright 2016 Han The Thanh (pdfTeX) et al.
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.21; using libpng 1.6.21
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with xpdf version 3.04
0
The stuff about the TeX version might well be different; it depends on which version of which TeX distribution you're using.
Most important is the last line; this is the return value of subprocess.call
and should be 0, indicating that the command return no error.
If the subprocess.call
raises an exception (like below; not sure if it will be the same on ms-windows), you need to modify the PATH
so that python can find LaTeX and the other things it needs.
>>> subprocess.call(['foo', '--version'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/local/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Edit: If this is not the problem, it could be that is it TeX that is not quitting. Try running the script. When you get the error dialog, look in the task manager if there is still a (la)tex process running. Look at the LaTeX code that you use, and try running it in LaTeX to check that it is actually valid. It might get hung on an error...
来源:https://stackoverflow.com/questions/41600714/why-does-python-become-unresponsive-when-i-use-latex-to-render-text