TransWikia.com

Include “contour” package into matlplotlib and set title using it

TeX - LaTeX Asked on April 18, 2021

I transfer the initial post from stackoverflow since one said it is rather a question for Tex exchange (I am going do delete the initial one). Here is this post :

In a matplotlib script, I try to write a text with latex style and especially by using contour package to add black contours on each letter and number (the letters are in yellow, so difficult to read them).

For this, I did at the beginning of script :

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}'])

and when I need to set the title :

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

I get the following error :

        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 commandn"
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 {1sigma }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.

The options ['pdflatex', '-interaction=nonstopmode', '-halt-on-error', seem to generate errors and I don’t understand why …

One Answer

EDIT: I don't think that the normal backend of matplotlib supports contour via text.usetex, as it then compiles to DVI and uses dvipng. If you change the backend you can get a working contour, see below.


  1. Your LaTeX code wouldn't work how you put it, even if your Python code was correct. It should be

    contour{black}{$color{yellow}bm{1sigma}pm 10$}
    
  2. The way you're setting the rcParams['text.latex.preamble'] of matplotlib is deprecated (it doesn't accept lists anymore, but only a single string), also you're missing a few packages necessary to make the output work.

The following is a complete Python script that works without error, but doesn't give the intended output (the contour doesn't work, and I currently have no idea why):

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{1sigma}pm' + str(10) + r'$}',
          fontsize=18)

plt.savefig('myfig.pdf')

Result:

enter image description here


If you set matplotlib's backend to 'pgf' you can get the results you want with the following:

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{1sigma}pm' + str(10) + r'$}',
          fontsize=18)

plt.savefig('myfig.pdf')

Result:

enter image description here

Correct answer by Skillmon on April 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP