gpt4 book ai didi

python - 如何让 matplotlib 和 latex 一起工作?

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:30 26 4
gpt4 key购买 nike

我有以下问题,我对 matplotlib 和 python 完全陌生:为了为我的论文制作高质量的图,我想使用 matplotlib 并且我想对轴等使用 latex 命令。当我尝试以下示例时(来自 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 等所做的:

sudo apt-get install python3
sudo apt-get install python-matplotlib
sudo apt-get build-dep python-matplotlib

我正在使用 texmaker 并认为这可能是问题所在;所以我做了以下事情:

sudo aptitude install texlive-fonts-recommended texlive-fonts-extra

如果我尝试任何其他绘图,包括 latex 命令,我会收到类似的错误消息,或者绘图没有任何轴标签。

最佳答案

您的问题之前已被视为 reported here on the matplotlib-user mailing list .决议is here并确认它有效 is here .

综上所述,需要清空tex.cache目录

要找到该目录的位置 - 进入 python 并执行 matplotlib.get_cachedir()

对于标准的 Ubuntu 14.04 安装 - 你应该得到如下内容:

~$ 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/*然后重新运行,它应该一切正常。


文档问题

usetex troubleshooting docs section 中提供了一些像这样的提示, 这导致 here找到目录,但不幸的是他们没有提到 get_cachedir()

编辑 Pull Request基于这个问题生成并合并到 matplotlib 文档中。

关于python - 如何让 matplotlib 和 latex 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com