使用带有LateX的matplotlib无法进行轴标记

时间:2016-03-30 11:06:52

标签: python matplotlib latex

我根据matplotlib文档中的示例({{3})在Ubuntu 14.04LTS,ghostscript(9.10)和dvipng(1.14)上安装了LateX(TeX 3.1415926(TeX Live 2013 / Debian)kpathsea版本6.1.1)。 })。如果我注释掉绘图的标签属性,一切正常,但如果没有,它们不会显示或者引发错误(请参阅下面的代码片段)。如何正确处理乳胶轴标记?我还需要一个包吗?

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.figure(figsize=(10, 7.5))
plt.xlabel(r'testx') # not showing up
plt.ylabel(r'testy',fontsize=16) #ValueError: Can only output finite numbers in PDF
plt.plot(t, s)
plt.title(r"\TeX\ is Number "
         r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
plt.subplots_adjust(top=0.8)
plt.grid()
plt.savefig('tex_demo.pdf')
plt.show()

先谢谢你,并提出最好的问候,clax

Beneath都是回溯,一个是savefig,另一个是show:

savefig:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    plt.savefig('tex_demo.pdf')
  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/backend_bases.py", line 1952, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 2352, in print_pdf
    self.figure.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/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_pdf.py", line 1782, in draw_tex
    self._setup_textpos(curx, cury, angle, oldx, oldy)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 1649, in _setup_textpos
    x,        y,         Op.textmatrix)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 548, in output
    self.write(fill(map(pdfRepr, data)))
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 144, in pdfRepr
    raise ValueError("Can only output finite numbers in PDF")
ValueError: Can only output finite numbers in PDF

显示:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
    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

0 个答案:

没有答案