Skip to content
New issue

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

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

Already on GitHub? # to your account

Relative axis coordinates #205

Open
andreassch opened this issue Oct 10, 2017 · 2 comments
Open

Relative axis coordinates #205

andreassch opened this issue Oct 10, 2017 · 2 comments

Comments

@andreassch
Copy link

Relative axis coordinates in text objects in matplotlib via transform=plt.gca().transAxes are not taken over in the generated pgfplots figure.

Python code for minimal example:

from matplotlib import pyplot as plt
from matplotlib2tikz import save as tikz_save

plt.plot([0, 10], [0, 10])
plt.text(0.5, 0.5, 'Centered text', transform=plt.gca().transAxes, horizontalalignment='center', verticalalignment='top')
#plt.savefig("matplotlib2tikz_text_relaxiscs.png", dpi=300)
tikz_save("matplotlib2tikz_text_relaxiscs.tikz")
plt.show()

The resulting matplotlib figure is:
matplotlib2tikz_text_relaxiscs
But the compiled pgfplots figure is:
matplotlib2tikz_text_relaxiscs_pgfplots

The generated pgfplots code is:

% This file was created by matplotlib2tikz v0.6.13.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
xmin=-0.5, xmax=10.5,
ymin=-0.5, ymax=10.5,
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0, forget plot]
table {%
0 0
10 10
};
\node at (axis cs:0.5,0.5)[
  scale=0.5,
  anchor=north,
  text=black,
  rotate=0.0
]{\bfseries Centered text};
\end{axis}

\end{tikzpicture}

The expected code in the \node command would be \node at (rel axis cs:0.5,0.5) to respect the relative coordinate system. Moreover, I would not expect the text to be scaled (i.e. no scale=0.5 key).

@Dustin1358
Copy link

Dustin1358 commented Apr 8, 2020

Can confirm the bug. Also adding "rel" before the axis solved the issue. For me the scale is 0.7 which looks good.

A fix for me which had no side effects as far as I can see is changing this line:
tikz_pos = f"(axis cs:{pos[0]:{ff}},{pos[1]:{ff}})"
in _text.py to:
tikz_pos = f"(rel axis cs:{pos[0]:{ff}},{pos[1]:{ff}})"

@HendrikLamar
Copy link

Probably obvious, but the same error appears with the plt.annotate command as well.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants