0

So, I'm drawing graphs using TikZ and pgfplots for my lecture notes. My question is about the two coordinate systems that are used in the following code:

\documentclass{article}

\usepackage{tikz} \usepackage{pgfplots} \begin{document}

\begin{tikzpicture} \begin{axis}[axis lines = center, xlabel = $x$, ylabel =$y$, ymax=3, ymin=-1, xmax=5, xmin=-1 ,samples=500, ytick={1,2}, xtick={1,2,3,4}] % Code for the graph. \addplot[blue, domain=2.05:4.5, thick] {-x + 4}; \addplot[blue, domain=-0.5:1.95, thick] {x}; \draw[blue] (300,297) circle[radius=1.5pt]; \node[above right] at (300,297) {\footnotesize $y=f(x)$}; \end{axis} \end{tikzpicture}

\end{document}

Question: Why is it that the lines of the graph can be drawn with the coordinates of the axes, but the coordinates of the nodes have to be specified using some other system?

Question: What is the other system? Is it easy to say where (0,0) in the "node system" is with respect to (0,0) on the axes?

Ideally I would like to be able to use the axes coordinates for all aspects of the diagram. But, any help at all would be great!

Thanks in advance.

Stefan Pinnow
  • 29,535
  • 1
    Hi, welcome. As mentioned in e.g. https://tex.stackexchange.com/a/208490/, if you add \pgfplotsset{compat=1.11}, axis coordinates are used for \node, \draw, etc. as well. – Torbjørn T. May 03 '21 at 22:26
  • @TorbjørnT. that's wonderful! Without that, where is the origin in the pgfplots coordinate system? Thanks for your response. –  May 03 '21 at 22:39

1 Answers1

1

As Torbjørn T. already mentioned in the comment below the question you can use the axis coordinate system also for TikZ stuff by adding \pgfplotsset{compat=1.11} to the preamble.

To (hopefully) answer the remaining questions please have a look at the following code and its comments.

More details can be found in the PGFPlots manual (v1.17). Have a look at

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat` level or higher to use `axis cs:` as default
        % coordinate system for TikZ stuff as well.
        compat=1.11,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % where to place the `axis` node in the TikZ coordinate system
        % (the default coordinate is (0,0)) ...
        at={(5mm,5mm)},
        % ... with the `anchor`
        % (the default is `south west`)
        anchor=outer south east,
        name=plot,
        % ---------------------------------------------------------------------
        xlabel=$x$,
        ylabel=$y$,
        legend pos=outer north east,
    ]
        \addplot {x^2};
        \legend{$x^2$}
    \end{axis}
% show the origin of the TikZ coordinate system
\fill [red,radius=2pt]   (0,0) circle;
% show the place where the `axis` node was placed
\draw [green,radius=4pt] (plot.outer south east) circle;
% "prove" that the `axis` node is really placed at the given coordinate
\draw [blue] (0,0) -- +(5mm,5mm);

\end{tikzpicture} \end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • 1
    +1, but you may emphasise tha it can be used newest version of pgfplots, for example recommended is recent one which is 1.17: \pgfplotsset{compat=1.17} ;-) – Zarko May 04 '21 at 05:21
  • @Zarko, that is true, that is what I would recommend too, but that is not * required* to answer the question. That that I hope that your two comments are enough. (If not, here another recommendation: Prefer stating an actual number as compat argument over the value newest (see https://tex.stackexchange.com/q/139690/95441), so also in 100 years you get the same result (from PGFPlots).) – Stefan Pinnow May 04 '21 at 05:34
  • @StefanPinnow thanks :) You've provided enough to get answers to all my questions. –  May 06 '21 at 00:50
  • You are welcome. – Stefan Pinnow May 06 '21 at 18:01