如何使matplotlib和乳胶一起工作? [英] How to get matplotlib and latex work together?
问题描述
我遇到以下问题,并且我对matplotlib和python完全陌生:要为我的论文生成高质量的图,我想使用matplotlib,并且想对轴等使用Latex命令.当我尝试以下示例时(来自 http://matplotlib.org/users/usetex.html#usetex-tutorial):
I have the following problem and I am totally new to matplotlib and python: To produce high qualitiy plots for my thesis I want to use matplotlib and I want to use the latex commands for the axes etc. When I try the following example (from http://matplotlib.org/users/usetex.html#usetex-tutorial):
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()
我收到以下消息:
File "test.py", line 21, in <module>
plt.savefig('tex_demo')
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig
return fig.savefig(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 505, in print_png
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1105, in draw
self.label.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 594, in draw
self._fontproperties, angle, mtext=self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 241, in draw_tex
self._renderer.draw_text_image(Z, x, y, angle, gc)
OverflowError: cannot convert float infinity to integer
我是Linux(Ubuntu 14.04)的新手,安装matplotlib时可能做错了什么;这是我完成安装matplotlib等的工作:
I'm new to Linux (Ubuntu 14.04) and maybe have done something wrong when installing matplotlib; here is what I have done to install matplotlib etc.:
sudo apt-get install python3
sudo apt-get install python-matplotlib
sudo apt-get build-dep python-matplotlib
我正在使用texmaker,并认为这可能是问题所在;所以我做了以下事情:
I am using texmaker and thought this could be the problem; so I did the following:
sudo aptitude install texlive-fonts-recommended texlive-fonts-extra
如果我尝试使用其他任何图形(包括乳胶命令),也会收到类似的错误消息,或者坐标轴上没有任何标签的图形.
If I try any other plots including latex commands I get similar error messages, or plots without any lables for the axes.
推荐答案
Your problem has been seen before as reported here on the matplotlib-user mailing list. The resolution is here and the confirmation that it works is here.
总而言之,您需要清除tex.cache目录.
In summary, you need to clear the tex.cache directory.
要查找该目录的位置-进入python并执行matplotlib.get_cachedir()
To find where that directory is - go into python and execute matplotlib.get_cachedir()
对于标准的Ubuntu 14.04安装-您应该获得以下内容:
For a standard Ubuntu 14.04 install - you should get something like the following:
~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl
>>> mpl.get_cachedir()
'/home/<username>/.cache/matplotlib'
然后执行rm <path/to/cache/from/above>/tex.cache/*
,然后重新运行,它应该都能正常工作.
Then do rm <path/to/cache/from/above>/tex.cache/*
and then re-run and it should all work.
usetex故障排除文档部分中提供了类似的提示, 此处来找到目录,但很遗憾,他们没有这样做不要提get_cachedir()
Some tips like this are available in the usetex troubleshooting docs section, which lead to here to locate the directory, but they unfortunately don't mention get_cachedir()
编辑 拉动请求根据此问题生成并合并到matplotlib文档中.
Edit Pull Request generated based on this question and merged into the matplotlib docs.
这篇关于如何使matplotlib和乳胶一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!