将“contour”包包含到 matlplotlib 中并使用它来设置标题

将“contour”包包含到 matlplotlib 中并使用它来设置标题

我从 stackoverflow 上转载了最初的帖子,因为有人说这更像是一个关于 Tex 交换的问题(我打算删除最初的帖子)。以下是该帖子:

在 matplotlib 脚本中,我尝试用 latex 样式编写文本,尤其是使用轮廓包在每个字母和数字上添加黑色轮廓(字母是黄色的,因此很难阅读)。

为此,我在脚本的开头做了以下操作:

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = ''.join([r'\usepackage[T1]{fontenc}'
                                           r'\usepackage{contour}'
                                           r'\usepackage{xcolor}'
                                           r'\usepackage{bm}'])

当我需要设置标题时:

ax.set_title(r'\contour{black}{$\color{yellow}\bm{1\sigma}\pm' + str(constraints) + r'$}', fontsize=18)

我收到以下错误:

        File "triplot_5_matrices_dev.py", line 318, in <module>
    g.fig.savefig('Comparison_triplot_FLAT_NO_GAMMA_dev.pdf', bbox_inches='tight')
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2089, in print_figure
    **kwargs)
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/matplotlib/backends/backend_pgf.py", line 898, in print_pdf
    self._print_pdf_to_fh(file, *args, **kwargs)
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/matplotlib/backends/backend_pgf.py", line 881, in _print_pdf_to_fh
    "figure.tex"], _log, cwd=tmpdir)
  File "/opt/intel/intelpython3/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 2136, in _check_and_log_subprocess
    f"The command\n"
RuntimeError: The command
    ['pdflatex', '-interaction=nonstopmode', '-halt-on-error', 'figure.tex']
failed and generated the following output:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/MacPorts 2020.54632_3) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./figure.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-03-06>
(/opt/local/share/texmf-texlive/tex/latex/base/minimal.cls
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
) (/opt/local/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/opt/local/share/texmf-texlive/tex/generic/iftex/ifvtex.sty
(/opt/local/share/texmf-texlive/tex/generic/iftex/iftex.sty)))
(/opt/local/share/texmf-texlive/tex/latex/base/fontenc.sty)
(/opt/local/share/texmf-texlive/tex/latex/contour/contour.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics/color.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics-cfg/color.cfg)
(/opt/local/share/texmf-texlive/tex/latex/graphics-def/pdftex.def))
(/opt/local/share/texmf-texlive/tex/latex/graphics/trig.sty)
Loading configuration file `contour.cfg'.
(/opt/local/share/texmf-texlive/tex/latex/contour/contour.cfg)
contour: Using driver file `pdftex.cnt'.
(/opt/local/share/texmf-texlive/tex/latex/contour/pdftex.cnt))
(/opt/local/share/texmf-texlive/tex/latex/xcolor/xcolor.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics-cfg/color.cfg))
(/opt/local/share/texmf-texlive/tex/latex/tools/bm.sty)
(/opt/local/share/texmf-texlive/tex/latex/pgf/basiclayer/pgf.sty
(/opt/local/share/texmf-texlive/tex/latex/pgf/utilities/pgfrcs.sty
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfutil-common.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfutil-common-lists.
tex))
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfutil-latex.def
(/opt/local/share/texmf-texlive/tex/latex/ms/everyshi.sty))
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfrcs.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/pgf.revision.tex)))
(/opt/local/share/texmf-texlive/tex/latex/pgf/basiclayer/pgfcore.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics/graphicx.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics/graphics.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics-cfg/graphics.cfg)))
(/opt/local/share/texmf-texlive/tex/latex/pgf/systemlayer/pgfsys.sty
(/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgfsys.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfkeys.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/utilities/pgfkeysfiltered.code.
tex)) (/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgf.cfg)
(/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgfsys-pdftex.def
(/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgfsys-common-pdf.d
ef)))
(/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgfsyssoftpath.code
.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/systemlayer/pgfsysprotocol.code
.tex))
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcore.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmath.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathcalc.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathutil.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathparser.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.code.tex
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.basic.cod
e.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.trigonome
tric.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.random.co
de.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.compariso
n.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.base.code
.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.round.cod
e.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.misc.code
.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfunctions.integerar
ithmetics.code.tex)))
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfmathfloat.code.tex))
(/opt/local/share/texmf-texlive/tex/generic/pgf/math/pgfint.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorepoints.code.t
ex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorepathconstruct
.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorepathusage.cod
e.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorescopes.code.t
ex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoregraphicstate.
code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoretransformatio
ns.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorequick.code.te
x)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoreobjects.code.
tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorepathprocessin
g.code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorearrows.code.t
ex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoreshade.code.te
x)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoreimage.code.te
x
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoreexternal.code
.tex))
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorelayers.code.t
ex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcoretransparency.
code.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorepatterns.code
.tex)
(/opt/local/share/texmf-texlive/tex/generic/pgf/basiclayer/pgfcorerdf.code.tex)
))
(/opt/local/share/texmf-texlive/tex/generic/pgf/modules/pgfmoduleshapes.code.te
x)
(/opt/local/share/texmf-texlive/tex/generic/pgf/modules/pgfmoduleplot.code.tex)

(/opt/local/share/texmf-texlive/tex/latex/pgf/compatibility/pgfcomp-version-0-6
5.sty)
(/opt/local/share/texmf-texlive/tex/latex/pgf/compatibility/pgfcomp-version-1-1
8.sty))
(/opt/local/share/texmf-texlive/tex/latex/l3backend/l3backend-pdfmode.def)
No file figure.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
(/opt/local/share/texmf-texlive/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/opt/local/share/texmf-texlive/tex/latex/epstopdf-pkg/epstopdf-base.sty
(/opt/local/share/texmf-texlive/tex/latex/latexconfig/epstopdf-sys.cfg))
ABD: EveryShipout initializing macros (./figure.pgf
(/opt/local/share/texmf-texlive/tex/latex/base/t1cmss.fd)

LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `OML/cmm/m/it' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `OMS/cmsy/m/n' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `OT1/cmr/bx/n' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `OML/cmm/b/it' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `OMS/cmsy/b/n' in size <8.5> not available
(Font)              size <8> substituted on input line 22562.


LaTeX Font Warning: Font shape `T1/cmss/m/n' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `OML/cmm/m/it' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OML/cmm/m/it' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `OMS/cmsy/m/n' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OMS/cmsy/m/n' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `OT1/cmr/bx/n' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OT1/cmr/bx/n' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `OML/cmm/b/it' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OML/cmm/b/it' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `OMS/cmsy/b/n' in size <20> not available
(Font)              size <20.74> substituted on input line 22656.


LaTeX Font Warning: Font shape `OMS/cmsy/b/n' in size <13.99994> not available
(Font)              size <14.4> substituted on input line 22656.


LaTeX Font Warning: Font shape `T1/cmss/m/n' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.

contour: Using 16 copies for `\(\displaystyle \color {yellow}\bm {1\sigma }\pm
0.0019\)' on input line 138807.

LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OT1/cmr/m/n' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.


LaTeX Font Warning: Font shape `OML/cmm/m/it' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OML/cmm/m/it' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.


LaTeX Font Warning: Font shape `OMS/cmsy/m/n' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OMS/cmsy/m/n' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.


LaTeX Font Warning: Font shape `OT1/cmr/bx/n' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OT1/cmr/bx/n' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.


LaTeX Font Warning: Font shape `OML/cmm/b/it' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OML/cmm/b/it' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.


LaTeX Font Warning: Font shape `OMS/cmsy/b/n' in size <18> not available
(Font)              size <17.28> substituted on input line 138807.


LaTeX Font Warning: Font shape `OMS/cmsy/b/n' in size <12.59995> not available
(Font)              size <12> substituted on input line 138807.

Runaway definition?
->\pgfsyssoftpath@movetotoken {227.15492pt}{335.86978pt}\pgfsyssoftpath@linetot
oken \ETC.
! TeX capacity exceeded, sorry [main memory size=5000000].
\pgfsyssoftpath@bigbuffer ...th@linetotoken {264.6
                                                  4189pt}{377.76115pt}\pgfsy...
l.187314 ...to{\pgfqpoint{3.944522in}{5.501139in}}
                                                  %
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on figure.log.

这些选项['pdflatex', '-interaction=nonstopmode', '-halt-on-error',似乎会产生错误,我不明白为什么......

答案1

编辑:我不认为 的常规后端matplotlib支持contourvia text.usetex,因为它随后会编译为 DVI 并使用dvipng。如果您更改后端,则可以获得一个可工作的contour,见下文。


  1. 即使你的 Python 代码是正确的,你的 LaTeX 代码也不会按照你所说的那样工作。它应该是

    \contour{black}{$\color{yellow}\bm{1\sigma}\pm 10$}
    
  2. rcParams['text.latex.preamble']您设置的方式matplotlib已被弃用(它不再接受列表,而只接受单个字符串),而且您缺少一些使输出工作所需的包。

以下是完整的 Python 脚本,它可以正常运行,但没有给出预期的输出(不起作用\contour,我目前不知道原因):

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = ''.join([r'\usepackage[T1]{fontenc}'
                                           r'\usepackage{contour}'
                                           r'\usepackage{xcolor}'
                                           r'\usepackage{bm}'])

fig = plt.figure()

plt.plot([0, 1], [0, 1])
plt.title(r'\contour{black}{$\color{yellow}\bm{1\sigma}\pm' + str(10) + r'$}',
          fontsize=18)

plt.savefig('myfig.pdf')

结果:

在此处输入图片描述


matplotlib如果将的后端设置为,则'pgf'可以通过以下方式获得所需的结果:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.use('pgf')
mpl.rcParams.update({
    'pgf.texsystem': 'pdflatex',
    'text.usetex': True,
    'pgf.preamble': ''.join([r'\usepackage[T1]{fontenc}'
                             r'\usepackage{contour}'
                             r'\usepackage{xcolor}'
                             r'\usepackage{bm}']),
    })

fig = plt.figure()

plt.plot([0, 1], [0, 1])
plt.title(r'\contour{black}{$\color{yellow}\bm{1\sigma}\pm' + str(10) + r'$}',
          fontsize=18)

plt.savefig('myfig.pdf')

结果:

在此处输入图片描述

相关内容