0

In pgfplots, I want to place the x^2 below the cos(x), as in the figure below.

I am trying to replicate the example from the manual, §4.19.3, but for some reason it seems that the syntax for node's positioning is different that that of tikz.

For example, in tikz, one can say

%\usetikzlibrary{positioning}
\node [below left=1pt and 2pt of n1.south west, anchor=south west] (n2) {node text};

to create a node n2 positioned relative to node n1.

  1. Can anyone tell me why in my code the two axiss are not one below the other?

  2. How can I add a yshift between the two axiss? (The package manual only mentions it and doesn't describe it)

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[name=myaxis,anchor=south east]
\addplot {cos(x)};
\end{axis}

\begin{axis}[at={(myaxis.below south west)},]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1 Answer 1

1

Probably the code given in the manual was a bit confusing - it's inside the code example, if you look carefully. Mainly you didn't tell where to anchor myaxis.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
 \begin{tikzpicture}
    \begin{axis}[name=myaxis,anchor=south east]
        \addplot {cos(x)};
    \end{axis}
    
    \begin{axis}[
        at={(myaxis.below south west)},
        anchor=north west,
        yshift=-5mm,
    ]
        \addplot {x^2};
    \end{axis}
 \end{tikzpicture}
\end{document}

result


P.S.: I suggest to use some indenting as we know from structured programming. Makes errors easier to spot and prevent. See e.g. Indentation Style.

6
  • 1
    Thanks very much! Can you explain the meaining of stating south west after below? Commented Jan 12 at 18:20
  • Sure. south west is a Tikz term to address the lower left corner of a rectangular. If you'd have the rare case of a square, you could (probably) also have said myaxis.below 225 , where it's located 225 deg counterclockwise starting from the horizontal x-axis. // See e.g. here in the Tutorial section of the tikz manual: tikz.dev/tutorial-nodes#sec-3.10 (look for anchor) Commented Jan 12 at 18:28
  • And can you show me how to use yshift and xshiftto control the distance between the two plots? Commented Jan 12 at 18:55
  • yshift is in the code for the second axis. Add xshift there and see what happens. Commented Jan 12 at 19:12
  • Another question: What is the meaning of specifying both below AND yshift=〈value〉? Shouldn't the below be ignored when followed by a yshift? BTW, I haven't seen this syntax in tikz. Commented Jan 12 at 21:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.