Positioning a pgfplot inside tikz (understand anchors, rotation and scale)

anchorpgfplotspositioningscalingtikz-pgf

I'm trying to rotate the plotting of a parabola above a tikzpicture. (For a parabola, I can achieve what I want without using pgfplots, but I need the same for more complicated plots.) I can't seen to understand how the anchors for rotation work, neither how does the tikz scaling impacts on them. What I've got so far:

with scale

without scale

The difference between the pictures is that the first has a scale=1.5 modifier added to the tikzpicture. The blue parabola should be where the black one is (i.e., with center at b). The green parabola is added only for visualization of a weird behavior.

  1. About the scale. I've seen this, but as far as I understand, I don't use outside elements in my pgfplot calculations (only the origin placement). Hence scaling after the plotting is done should be no problem. Furthermore, as seen in the second figure, even when the scale input is removed, the positioning and size of the parabolas is still incorrect. Why is it that the scale is not working properly with the pgfplot? How is it possible that the scaling moves the point (0,0) upwards or downwards ("weird behaviour")? (maybe the second question is related to bad anchoring?)
    Moreover, why is the blue parabola so big, even without scaling, if the plot domain is the same used for the black parabola?

  2. About the rotation. I took the approach from here and it seems to work fine, although with the wrong anchor. However, I don't understand fully its statement. I get that rotate around={90:(current axis.origin)} rotates around the second entry, (current axis.origin). But what is the current axis? What other options could I write instead of that?
    I couldn't find anything about rotating the whole plot in the pgfplots manual.

  3. About the anchoring. It doesn't work and I don't understand why not. Without the at option, the misplacement is even worse (and I actually need that option, because I want to manually set the plot placement, as done here). My understanding: the at option fixes a point A in the tikzpicture coordinates; then, the anchor option takes a location B of the pgfplot coordinates and fixes it in the tikzpicture, directly over A. Is this correct? If so, why is my code not working? The point (0,0) is in the middle of a and b and therefore placing the plot origin at (0,0) and afterwards rotating the plot around its origin should be enough to place the blue parabola over the black one (and the green parabola over a).

Of course a running code is welcome, but mostly I want to understand how this placement works, so that I can use it for other complicated plots as well. Thank you in advance!

MWE:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
%\pgfplotsset{compat=1.15} %this line removes the warning but cuts the picture

\begin{document}
\begin{tikzpicture}
    [scale=1.5] %remove this line for figure 2
    
    \coordinate (a) at (-1,0);
    \coordinate (b) at (1,0);
    \coordinate (o) at (0,0);
    
    %draw reference line
    \filldraw (a) node[below]{$a$} (a) {} circle (0.3pt);
    \filldraw (b) node[below]{$b$} (b) {} circle (0.3pt);
    \draw[-] (-1.5,0)--(1.5,0);
    
    %these are out of scale and misplaced
    \begin{axis}[at={(o)}, %where the origin is supposed to be
        anchor=origin, % i have tried center as well
        rotate around={-90:(current axis.origin)}, % Supposed to rotate around the origin
        domain=-1:1,
        hide axis]
        \addplot[blue] {x^2+1};
    \end{axis}

    \begin{axis}[at={(o)}, %where the origin is supposed to be
            anchor=origin, % i have tried center as well
            rotate around={90:(current axis.origin)}, % Supposed to rotate around the origin
            domain=-1:1,
            hide axis]
            \addplot[green] {x^2+1};
    \end{axis}
    
    %what it should be
    \draw[black, rotate=-90]   plot[smooth,domain=-1:1] (\x, {(\x)^2+1});
\end{tikzpicture}
\end{document}

Best Answer

To scale the plot, adjust the width (height).

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
%\pgfplotsset{compat=1.15} %this line removes the warning but cuts the picture

\begin{document}
\begin{tikzpicture}
    %[scale=1.5] %remove this line for figure 2
    
    \begin{axis}[scale only axis, axis equal image, width=4cm,
        rotate=90,
        domain=-1:1,
        hide axis]
        \addplot[blue] {x*x+1};
        \addplot[green] {-1-x*x};
        \coordinate (a) at (axis cs: 0,1);
        \coordinate (b) at (axis cs: 0,-1);
    \end{axis}
   
    %draw reference line
    \filldraw (a) node[below]{$a$} {} circle (0.3pt);
    \filldraw (b) node[below]{$b$} {} circle (0.3pt);
    \draw[-] ($(a)!1.5!(b)$)--($(b)!1.5!(a)$);
 
\end{tikzpicture}
\end{document}

demo


This attempts to move the pgfplots instead. Not quite sure where the small offsets are coming from.

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
%\pgfplotsset{compat=1.15} %this line removes the warning but cuts the picture

\newsavebox{\tempboxA}
\newsavebox{\tempboxB}

\begin{document}
\savebox{\tempboxA}{\begin{tikzpicture}
  \begin{axis}[scale only axis, axis equal image, height=2cm,
        rotate=-90,
        domain=-1:1,
        hide axis]
        \addplot[blue] {x*x+1};
        \coordinate (A) at (axis cs:0,0);
  \end{axis}
  \begin{pgfinterruptboundingbox}
    \path (A);
    \pgfgetlastxy{\xA}{\yA}
    \global\let\xA=\xA
    \global\let\yA=\yA
  \end{pgfinterruptboundingbox}
\end{tikzpicture}}%
%
\savebox{\tempboxB}{\begin{tikzpicture}
  \begin{axis}[scale only axis, axis equal image, height=2cm,
        rotate=90,
        domain=-1:1,
        hide axis]
        \addplot[green] {x*x+1};
        \coordinate (B) at (axis cs:0,0);
  \end{axis}
  \begin{pgfinterruptboundingbox}
    \path (B);
    \pgfgetlastxy{\xB}{\yB}
    \global\let\xB=\xB
    \global\let\yB=\yB
  \end{pgfinterruptboundingbox}
\end{tikzpicture}}%
%
\begin{tikzpicture}
    %[scale=1.5] %remove this line for figure 2
    
    \coordinate (a) at (-1,0);
    \coordinate (b) at (1,0);
    \coordinate (o) at (0,0);
    
    %draw reference line
    \filldraw (a) node[below]{$a$} {} circle (0.3pt);
    \filldraw (b) node[below]{$b$} {} circle (0.3pt);
    \draw[-] (-1.5,0)--(1.5,0);
    
    \node[above right, xshift={-\xA}, yshift={-\yA}, inner sep=0pt] at (o) {\usebox\tempboxA};
    \node[above right, xshift={-\xB}, yshift={-\yB}, inner sep=0pt] at (o) {\usebox\tempboxB};
    
    %what it should be
    \draw[black, rotate=-90]   plot[smooth,domain=-1:1] (\x, {(\x)^2+1});
\end{tikzpicture}
\end{document}