diff --git a/src/tikzplotlib/_save.py b/src/tikzplotlib/_save.py index d89cadfa..60aa5ac0 100644 --- a/src/tikzplotlib/_save.py +++ b/src/tikzplotlib/_save.py @@ -141,9 +141,9 @@ def get_tikz_code( The following optional attributes of matplotlib's objects are recognized and handled: - - axes.Axes._tikzplotlib_anchors + - `axes.Axes._tikzplotlib_anchors` This attribute can be set to a list of ((x,y), anchor_name) tuples. - Invisible nodes at the respective location will be created which can be + Invisible nodes at the respective location will be created which can be referenced from outside the axis environment. """ # not as default value because gcf() would be evaluated at import time @@ -351,6 +351,10 @@ def _recurse(data, obj): # Run through the child objects, gather the content. data, children_content = _recurse(data, child) + if hasattr(child, "_tikzplotlib_anchors"): + for (x, y), anchor_name in child._tikzplotlib_anchors: + children_content.append(f"\\coordinate ({anchor_name}) at (axis cs:{x},{y});\n") + # populate content and add axis environment if desired if data["add axis environment"]: content.extend( diff --git a/tests/test_coordinates.py b/tests/test_coordinates.py new file mode 100644 index 00000000..56f6006e --- /dev/null +++ b/tests/test_coordinates.py @@ -0,0 +1,17 @@ +def plot(): + from matplotlib import pyplot as plt + + fig = plt.figure() + plt.plot([1, 2, 3], [4, -2, 3]) + ax = plt.gca() + ax._tikzplotlib_anchors = [ + ((1.5, 2.5), "foo"), + ((0.4, 1.3), "bar"), + ] + return fig + + +def test(): + from .helpers import assert_equality + + assert_equality(plot, __file__[:-3] + "_reference.tex") diff --git a/tests/test_coordinates_reference.tex b/tests/test_coordinates_reference.tex new file mode 100644 index 00000000..ea9ff3c1 --- /dev/null +++ b/tests/test_coordinates_reference.tex @@ -0,0 +1,27 @@ +% This file was created with tikzplotlib v0.10.1. +\begin{tikzpicture} + +\definecolor{darkgray176}{RGB}{176,176,176} +\definecolor{steelblue31119180}{RGB}{31,119,180} + +\begin{axis}[ +tick align=outside, +tick pos=left, +x grid style={darkgray176}, +xmin=0.9, xmax=3.1, +xtick style={color=black}, +y grid style={darkgray176}, +ymin=-2.3, ymax=4.3, +ytick style={color=black} +] +\addplot [semithick, steelblue31119180] +table {% +1 4 +2 -2 +3 3 +}; +\coordinate (foo) at (axis cs:1.5,2.5); +\coordinate (bar) at (axis cs:0.4,1.3); +\end{axis} + +\end{tikzpicture}