11import matplotlib as mpl
22import numpy
3- from matplotlib .backends import backend_pgf as mpl_backend_pgf
3+ from matplotlib .backends .backend_pgf import (
4+ common_texification as mpl_common_texification ,
5+ )
46
57from . import color
68
79
10+ def _common_texification (string ):
11+ # Work around <https://github.com/matplotlib/matplotlib/issues/15493>
12+ return mpl_common_texification (string ).replace ("&" , "\\ &" )
13+
14+
815class Axes :
916 def __init__ (self , data , obj ): # noqa: C901
1017 """Returns the PGFPlots code for an axis environment.
@@ -36,13 +43,14 @@ def __init__(self, data, obj): # noqa: C901
3643 title = obj .get_title ()
3744 data ["current axis title" ] = title
3845 if title :
46+ title = _common_texification (title )
3947 self .axis_options .append (u"title={{{}}}" .format (title ))
4048
4149 # get axes titles
4250 xlabel = obj .get_xlabel ()
4351 xrotation = obj .xaxis .get_label ().get_rotation ()
4452 if xlabel :
45- xlabel = mpl_backend_pgf . common_texification (xlabel )
53+ xlabel = _common_texification (xlabel )
4654 self .axis_options .append (u"xlabel={{{}}}" .format (xlabel ))
4755 if xrotation != 0 :
4856 self .axis_options .append (
@@ -51,7 +59,7 @@ def __init__(self, data, obj): # noqa: C901
5159 ylabel = obj .get_ylabel ()
5260 yrotation = obj .yaxis .get_label ().get_rotation ()
5361 if ylabel :
54- ylabel = mpl_backend_pgf . common_texification (ylabel )
62+ ylabel = _common_texification (ylabel )
5563 self .axis_options .append (u"ylabel={{{}}}" .format (ylabel ))
5664 if yrotation != 90 :
5765 self .axis_options .append (
@@ -558,7 +566,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
558566 # store the label anyway
559567 label = ticklabel .get_text ()
560568 if ticklabel .get_visible ():
561- label = mpl_backend_pgf . common_texification (label )
569+ label = _common_texification (label )
562570 pgfplots_ticklabels .append (label )
563571 else :
564572 is_label_required = True
0 commit comments