Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError in matplotlib when using \: LaTeX horizontal spacing

See MWE below:

import matplotlib.pyplot as plt
import matplotlib.offsetbox as offsetbox

fig = plt.figure()
ax = fig.add_subplot(111)

text1 = r'$a\,=\,{}\pm{}$'.format(0.01, 0.002)  # Works.
text2 = r'$a\;=\,{}\pm{}$'.format(0.01, 0.002)  # Works.
text3 = r'$a\:=\,{}\pm{}$'.format(0.01, 0.002)  # Does not work.

text = text3
ob = offsetbox.AnchoredText(text, pad=1, loc=6, prop=dict(size=13))
ob.patch.set(alpha=0.85)
ax.add_artist(ob)

plt.show()

While text1 and text2 which use \, and \; spacing will work without issues, text3 which uses \: (ie: the horizontal spacing located between the previous two) fails with a ValueError:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 42, in on_draw_event
    self._render_figure(w, h)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 32, in _render_figure
    backend_agg.FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 469, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1085, in draw
    func(*args)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2110, in draw
    a.draw(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py", line 1107, in draw
    bbox = self.get_window_extent(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py", line 1063, in get_window_extent
    w, h, xd, yd = self.get_extent(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py", line 1014, in get_extent
    w, h, xd, yd = self.get_child().get_extent(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/offsetbox.py", line 782, in get_extent
    bbox, info, d = self._text._get_layout(renderer)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/text.py", line 320, in _get_layout
    ismath=ismath)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 228, in get_text_width_height_descent
    self.mathtext_parser.parse(s, self.dpi, prop)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/mathtext.py", line 3005, in parse
    box = self._parser.parse(s, font_output, fontsize, dpi)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/mathtext.py", line 2339, in parse
    six.text_type(err)]))
ValueError: 
a\:=\,0.01\pm0.002
 ^
Unknown symbol: \ (at char 1), (line:1, col:2)

Why is this LaTeX spacing not recognized?

like image 638
Gabriel Avatar asked May 28 '15 21:05

Gabriel


1 Answers

Because matplotlib uses mathtext as default math renderer. Mathtext is a subset of regular TeX. You can use full LaTeX with:

from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}'] #if needed
plt.plot([1,2,3])
plt.title(r"$a\:=\,{}\pm{}$".format(0.01, 0.002))
plt.show()
like image 71
317070 Avatar answered Nov 14 '22 22:11

317070