Skip to content

Add support for axes.Axes._tikzplotlib_anchors #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions src/tikzplotlib/_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_coordinates.py
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 27 additions & 0 deletions tests/test_coordinates_reference.tex
Original file line number Diff line number Diff line change
@@ -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}