matplotlib does not display latex fonts correctly

ぐ巨炮叔叔 提交于 2019-12-11 13:04:36

问题


I'm having a problem with using latex fonts in matplotlib, in so far that they do not show up in interactive windows. Any help would be much appreciated :)

I believe the issue did not exist on my system before, maybe a software update messed something up last month..

The default Latex font works without problems.

Utopia / Fourier is broken, for example the following example (I replaced my home directory by a tilde in the output, everything else is just as I got it):

import matplotlib.pyplot as plt
plt.rcParams.update({
    'text.usetex': True,
    'text.latex.preamble': [
        r"\usepackage{fourier}",
    ]
})
plt.plot([1,2],[3,4])
plt.xlabel('$x$')
plt.savefig('test.pdf')

gives the expected output in pdf, but the interactive window has no text:

I get a lot or warnings (I assume one for every text item, or for every glyph?):

Python 2.7.5+ (default, Feb 27 2014, 19:39:55) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information.

Imported NumPy 1.7.1, SciPy 0.12.0, Matplotlib 1.2.1
Type "scientific" for more details.
>>> runfile(r'~/Desktop/Untitled Python Script.py', wdir=r'~/Desktop')
dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 futr8t
mktexpk: don't know how to create bitmap font for futr8t.
mktexpk: perhaps futr8t is missing from the map file.
kpathsea: Appending font creation commands to missfont.log.
dvipng warning: font futr8t at 384 dpi not found, characters will be left blank dvipng warning: unable to draw glyph 49, skipping 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 futmii
mktexpk: don't know how to create bitmap font for futmii.
mktexpk: perhaps futmii is missing from the map file.
dvipng warning: font futmii at 384 dpi not found, characters will be left blank dvipng warning: unable to draw glyph 58, skipping 

the files which dvipng cannot find are available on my system:

$ locate ps2pk.map
~/.texlive2013/texmf-var/fonts/map/dvips/updmap/ps2pk.map
~/.texmf-var/fonts/map/dvips/updmap/ps2pk.map
/usr/local/texlive/2013/texmf-dist/fonts/map/dvips/updmap/ps2pk.map
/usr/local/texlive/2013/texmf-var/fonts/map/dvips/updmap/ps2pk.map
$ locate psfonts.map
~/.texlive2013/texmf-var/fonts/map/dvips/updmap/psfonts.map
~/.texmf-var/fonts/map/dvips/updmap/psfonts.map
/usr/local/texlive/2013/texmf-dist/fonts/map/dvips/updmap/psfonts.map
/usr/local/texlive/2013/texmf-var/fonts/map/dvips/updmap/psfonts.map

Another try, with Concrete and Euler gives errors instead of warnings:

import matplotlib.pyplot as plt
plt.rcParams.update({
    'text.usetex': True,
    'text.latex.preamble': [
        r"\usepackage{ccfonts,eulervm}",
        r"\usepackage[T1]{fontenc}",
    ]
})
plt.plot([1,2],[3,4])
plt.xlabel('$x$')
plt.savefig('test.pdf')

this gives the following output:

Python 2.7.5+ (default, Feb 27 2014, 19:39:55) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Imported NumPy 1.7.1, SciPy 0.12.0, Matplotlib 1.2.1
Type "scientific" for more details.
>>> runfile(r'~/Desktop/Untitled Python Script.py', wdir=r'~/Desktop')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 538, in runfile
    execfile(filename, namespace)
  File "~/Desktop/Untitled Python Script.py", line 17, in <module>
    plt.savefig('test.pdf')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 472, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1370, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_qt4agg.py", line 161, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2096, in print_figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1848, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 2307, in print_pdf
    file.close()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 507, in close
    self.writeFonts()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 604, in writeFonts
    realpath, stat_key = get_realpath_and_stat(filename)
  File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 796, in __call__
    stat = os.stat(realpath)
OSError: [Errno 2] No such file or directory: '~/Desktop/ccr10'
>>> dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 ccr10
mktexpk: Running mf-nowin -progname=mf \mode:=cx; mag:=1+84/300; nonstopmode; input ccr10
This is METAFONT, Version 2.718281 (TeX Live 2013)

(/usr/local/texlive/2013/texmf-dist/fonts/source/public/concrete/ccr10.mf
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/cmbase.mf)
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/roman.mf
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romanu.mf [65]
[66] [67] [68] [69] [70] [71] [72] [73] [74] [75] [76] [77] [78] [79] [80]
[81] [82] [83] [84] [85] [86] [87] [88] [89] [90])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romanl.mf [97]
[98] [99] [100] [101] [102] [103] [104] [105] [106] [107] [108] [109] [110]
[111] [112] [113] [114] [115] [116] [117] [118] [119] [120] [121] [122])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/greeku.mf [0]
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romand.mf [48]
[49] [50] [51] [52] [53] [54] [55] [56] [57])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romanp.mf [36]
[38] [63] [62])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romspl.mf [16]
[17] [25] [26] [27] [28])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romspu.mf [29]
[30] [31]) (/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/punct.mf
[33] [60] [35] [37] [39] [40] [41] [42] [43] [44] [46] [47] [58] [59] [61]
[64] [91] [93] [96])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/accent.mf [18]
[19] [20] [21] [22] [23] [24] [32] [94] [95] [125] [126] [127])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/romlig.mf [11]
[12] [13] [14] [15])
(/usr/local/texlive/2013/texmf-dist/fonts/source/public/cm/comlig.mf [34]
[45] [92] [123] [124]) ) )
Font metrics written on ccr10.tfm.
Output written on ccr10.384gf (128 characters, 15368 bytes).
Transcript written on ccr10.log.
mktexpk: ~/.texlive2013/texmf-var/fonts/pk/cx/public/concrete/ccr10.384pk: successfully generated.
dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 ccr10
mktexpk: ~/.texlive2013/texmf-var/fonts/pk/cx/public/concrete/ccr10.384pk already exists.
dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 ccr10
mktexpk: ~/.texlive2013/texmf-var/fonts/pk/cx/public/concrete/ccr10.384pk already exists.
dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 ccr10
mktexpk: ~/.texlive2013/texmf-var/fonts/pk/cx/public/concrete/ccr10.384pk already exists.
dvipng warning: cannot find ps2pk.map, nor psfonts.map 
kpathsea: Running mktexpk --mfmode cx --bdpi 300 --mag 1+84/300 --dpi 384 ccr10
mktexpk: ~/.texlive2013/texmf-var/fonts/pk/cx/public/concrete/ccr10.384pk already exists.
dvipng warning: cannot find ps2pk.map, nor psfonts.map 

etcetera

来源:https://stackoverflow.com/questions/22597231/matplotlib-does-not-display-latex-fonts-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!