Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- id: black
language_version: python3
Expand Down
9 changes: 1 addition & 8 deletions src/tikzplotlib/_axes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import matplotlib as mpl
import numpy as np
from matplotlib.backends.backend_pgf import (
common_texification as mpl_common_texification,
)

from . import _color


def _common_texification(string):
# Work around <https://github.com/matplotlib/matplotlib/issues/15493>
return mpl_common_texification(string).replace("&", "\\&")
from ._util import _common_texification


class Axes:
Expand Down
2 changes: 0 additions & 2 deletions src/tikzplotlib/_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

def draw_legend(data, obj):
"""Adds legend code."""
texts = []
children_alignment = []
for text in obj.texts:
texts.append(f"{text.get_text()}")
children_alignment.append(f"{text.get_horizontalalignment()}")

# Get the location.
Expand Down
9 changes: 7 additions & 2 deletions src/tikzplotlib/_line2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from . import _files
from . import _path as mypath
from ._markers import _mpl_marker2pgfp_marker
from ._util import get_legend_text, has_legend, transform_to_data_coordinates
from ._util import (
_common_texification,
get_legend_text,
has_legend,
transform_to_data_coordinates,
)


def draw_line2d(data, obj):
Expand Down Expand Up @@ -100,7 +105,7 @@ def draw_line2d(data, obj):
content += c

if legend_text is not None:
content.append(f"\\addlegendentry{{{legend_text}}}\n")
content.append(f"\\addlegendentry{{{_common_texification(legend_text)}}}\n")

return data, content

Expand Down
3 changes: 2 additions & 1 deletion src/tikzplotlib/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import _path as mypath
from ._text import _get_arrow_style
from ._util import _common_texification


def draw_patch(data, obj):
Expand Down Expand Up @@ -146,7 +147,7 @@ def _draw_rectangle(data, obj, draw_options):
data["rectangle_legends"].add(label)
draw_opts = ",".join(draw_options)
cont += f"\\addlegendimage{{ybar,ybar legend,{draw_opts}}}\n"
cont += f"\\addlegendentry{{{label}}}\n\n"
cont += f"\\addlegendentry{{{_common_texification(label)}}}\n\n"
return data, cont


Expand Down
4 changes: 2 additions & 2 deletions src/tikzplotlib/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._axes import _mpl_cmap2pgf_cmap
from ._hatches import _mpl_hatch2pgfp_pattern
from ._markers import _mpl_marker2pgfp_marker
from ._util import get_legend_text, has_legend
from ._util import _common_texification, get_legend_text, has_legend


def draw_path(data, path, draw_options=None, simplify=None):
Expand Down Expand Up @@ -319,7 +319,7 @@ def draw_pathcollection(data, obj):
content.append("};\n")

if legend_text is not None:
content.append(f"\\addlegendentry{{{legend_text}}}\n")
content.append(f"\\addlegendentry{{{_common_texification(legend_text)}}}\n")

return data, content

Expand Down
8 changes: 8 additions & 0 deletions src/tikzplotlib/_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import matplotlib.transforms
import numpy as np
from matplotlib.backends.backend_pgf import (
common_texification as mpl_common_texification,
)


def has_legend(axes):
Expand Down Expand Up @@ -39,3 +42,8 @@ def transform_to_data_coordinates(obj, xdata, ydata):
)
return transform.transform(points).T
return xdata, ydata


def _common_texification(string):
# Work around <https://github.com/matplotlib/matplotlib/issues/15493>
return mpl_common_texification(string).replace("&", "\\&")
3 changes: 2 additions & 1 deletion tests/test_escape_chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

def plot():
fig = plt.figure()
plt.plot(0, 0, "kx")
plt.plot(0, 0, "kx", label="Foo & Bar Dogs_N_Cats")
plt.title("Foo & Bar Dogs_N_Cats")
plt.xlabel("Foo & Bar Dogs_N_Cats")
plt.ylabel("Foo & Bar Dogs_N_Cats")
plt.gca().legend()
return fig


Expand Down
4 changes: 4 additions & 0 deletions tests/test_escape_chars_reference.tex
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
\begin{tikzpicture}

\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{lightgray204}{RGB}{204,204,204}

\begin{axis}[
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=lightgray204},
tick align=outside,
tick pos=left,
title={Foo \& Bar Dogs\_N\_Cats},
Expand All @@ -19,6 +22,7 @@
table {%
0 0
};
\addlegendentry{Foo \& Bar Dogs\_N\_Cats}
\end{axis}

\end{tikzpicture}