3

This is a further follow up to this question:

When using a shifted scope within an axis environment, the (0,0) coordinate is not at the position where I shifted the scope to:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0}]
    \begin{axis}[
        xmin=20,
        xmax=170,
        ymin=0,
        ymax=130]
    \node[green] at (160,83) {x};
    \begin{scope}[shift={(160,83)}]
        \node[red] at (0,0) {x};
    \end{scope}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Thanks to the linked question I managed to get it working by "redefining" the axis scales:

\begin{scope}[shift={(160,83)}, x={(1,0)}, y={(0,1)}]

enter image description here

But why is this needed? Can anyone please explain this behavior?

Stefan Pinnow
  • 29,535
F1iX
  • 358

1 Answers1

3

The axis coordinate (0,0) is not at the coordinates (0,0) of the ambient tikzpicture, so you need to account for that. Here is one way of doing this.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0}]
    \begin{axis}[
        xmin=20,
        xmax=170,
        ymin=0,
        ymax=130]
    \node[green] at (160,83) {x};
    \begin{scope}[shift={($(160,83)-(0,0)$)}]
        \node[red] at (0,0) {o};
    \end{scope}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you, this works! But how can I now rotate the scope? \begin{scope}[shift={($(160,83)-(0,0)$)}, rotate=90] \node[red] at (0,0) {o}; \node[black] at (10,0) {x}; does not work as intended... – F1iX Jan 05 '20 at 14:55
  • 2
    @F1iX It is the same reason why it does not work. You need \begin{scope}[shift={($(160,83)-(0,0)$)}, rotate around={90:(0,0)}] \node[red] at (0,0){o}; \node[black] at (10,0) {x}; \end{scope}, i.e. rotate around the (0,0) of the axis. –  Jan 05 '20 at 15:05