1

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

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

  • Ok, the error you're facing in the end is that you have exceeded TeX's capacity. This sometimes happens with matplotlib if you got too much data for pdfTeX to handle. You have two options. 1. Increase pdfTeX's memory size (it allocates statically), see e.g. here. 2. Use LuaTeX instead (by setting 'pgf.texsystem' to 'lualatex'). – Skillmon Jan 02 '21 at 09:10
  • Were you able to fix the remaining issues? – Skillmon Jan 09 '21 at 10:19
  • @Skillmon Yes, thanks to you. I have now for ask another question since I would like to set the text of legend with Latex font style. Regards. –  Jan 09 '21 at 12:55

1 Answers1

2

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

plt.savefig('myfig.pdf')

Result:

enter image description here

Skillmon
  • 60,462
  • Thanks for your quick answer. Unfortunately, it doesn't work. I tried by doing : ax = g.subplots[m,n] then ax.set_title(r'\contour{black}{$\color{yellow}\bm{1\sigma}\pm'+str(constraints)+r'}$', fontsize=18) and it gives following error : RuntimeError: latex was not able to process the following string: b'\\\\contour{black}{$\\\\color{yellow}\\\\bm{1\\\\sigma}\\\\pm0.0019}$' ... –  Dec 30 '20 at 12:42
  • Could you take a look please at my UPDATE1 ? I am on macOS 11.1 –  Dec 30 '20 at 13:16
  • @youpilat13 you marked my answer as accepted. Does that mean your issue was solved in the meantime with my second code block or do you still have issues? – Skillmon Dec 30 '20 at 16:33
  • I marked your answer since you have provided help and I am convinced this should be the solution but your solution doesn't work yet for me. I have put the errors in my UPDATE 1. if you could take a look at this, it would be fine. Regards –  Dec 30 '20 at 17:15
  • If I can't conclude, i will launch a bounty, maybe experts like you will be able to solve the compilation of latex in this python script –  Dec 30 '20 at 17:17
  • @youpilat13 I have no way of guessing what is going wrong for you, the output of your LaTeX run isn't complete and the snippet you showed doesn't contain an error. – Skillmon Jan 01 '21 at 22:28
  • hello, I have launched a bounty and set all the output error log. Hoping someone will understand where the error could come from. Regards –  Jan 02 '21 at 00:04
  • Thanks for your answer. Is there a way to change the width of black border on yellow text ? –  Jan 06 '21 at 12:37
  • @youpilat13 add r'\contourlength{0.06em}' to your 'pgf.preamble' to double the width of the contour. – Skillmon Jan 06 '21 at 13:31