使用对数轴刻度时的Matplotlib乳胶错误,取决于版本

时间:2018-09-07 12:06:42

标签: python matplotlib latex

在python 2.7.15中运行以下命令时,我在matplotlib中遇到了乳胶错误。使用对数轴刻度时会发生这种情况。是否出现错误取决于matplotlib版本,我使用1.5.1(不发生错误)和2.2.3(发生错误)进行了测试。仅当代码在单个块中执行时(如在函数中),也会发生该错误。请参阅下面的最小示例(在两个单独的python控制台中执行!)。使用新的matplotlib和tex模式时,到底是什么导致了错误,我该如何避免呢?

两种情况的通用代码:

import sys
default_sys_path = sys.path
import numpy as np

def reproduce_error(old_matplotlib, outname):
    if old_matplotlib == True:
        sys.path = ['/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages'] + default_sys_path
    else:
        sys.path = ['/scratch/seismo/proxauf/conda/lib/python2.7/site-packages'] + default_sys_path

    import matplotlib
    import matplotlib.pyplot as plt
    print matplotlib.__version__
    print matplotlib.__path__

    plt.rcParams['text.usetex'] = True
    plt.semilogy(np.arange(1,10)*10**(-10))
    plt.savefig('/home/proxauf/%s' % outname)
    plt.close()
    plt.rcParams['text.usetex'] = False

案例1的代码和输出(matplotlib 1.5.1):

reproduce_error(old_matplotlib=True, outname='test_mathdefault_old.pdf')

1.5.1
['/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib']

案例2的代码和输出(matplotlib 2.2.3):

reproduce_error(old_matplotlib=False, outname='test_mathdefault_new.pdf')

2.2.3
['/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib']
Traceback (most recent call last):
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
    self.draw()
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 437, in draw
    self.figure.draw(self.renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/figure.py", line 1493, in draw
    renderer, self, artists, self.suppressComposite)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2635, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1192, in draw
    renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 236, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 365, in make_dvi
    texfile], tex)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 344, in _run_checked_subprocess
    exc=exc.output.decode('utf-8')))
RuntimeError: latex was not able to process the following string:
'$\\\\mathdefault{10^{-10}}$'
Here is the full report generated by latex:
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/TeX Live for SUSE Linux)
 restricted \write18 enabled.
entering extended mode
(/home/proxauf/.cache/matplotlib/tex.cache/be547c40948f52354492209662050ad0.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/base/textcomp.sty
(/usr/share/texmf/tex/latex/base/ts1enc.def))
(/usr/share/texmf/tex/latex/geometry/geometry.sty
(/usr/share/texmf/tex/latex/graphics/keyval.sty)
(/usr/share/texmf/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texmf/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texmf/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
    `width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
    `height' (5058.9pt) is ignored.
) (./be547c40948f52354492209662050ad0.aux)
(/usr/share/texmf/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Undefined control sequence.
<recently read> \mathdefault 

l.13 ...000000}{12.500000}{\sffamily $\mathdefault
                                                  {10^{-10}}$}
No pages of output.
Transcript written on be547c40948f52354492209662050ad0.log.

1 个答案:

答案 0 :(得分:0)

据我了解,错误是由于命令\ mathdefault引起的。这是python中定义的命令,no是Latex的基本命令。因此,要解决此问题,您需要在plt.semilogy(np.arange(1,10)* 10 **(-10))上方放置以下命令:

from matplotlib import rcParams
rcParams['text.latex.preamble'] = r'\newcommand{\mathdefault}[1][]{}'

因此,命令\ mathdefault将被定义并且什么都不做。

我用这些建议重写了您的代码,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = r'\newcommand{\mathdefault}[1][]{}'
plt.semilogy(np.arange(1,10)*10**(-10))
# plt.savefig('/home/proxauf/%s' % outname) ## outname????
plt.close()
rcParams['text.usetex'] = False