4

Why does the following code draw an inclined line? I thought the sum ($(1,6) + (2,0)$) would return a point (3,6).

\documentclass[dvipsnames]{article}\usepackage[utf8]{inputenc}\usepackage{xcolor}\usepackage{pgfplots,tikz}\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}\pgfplotsset{compat=newest}\usepgflibrary{arrows}                                                  \usepgfplotslibrary{fillbetween}\begin{document}\begin{tikzpicture}  \begin{axis}[ axis lines = middle,                xmin = -1,                ymin = -5,                xmax = 10,                ymax = 14,                domain = -1:10,                xtick = {1,2,...,9},                ytick = \empty,                xlabel style={below right},                ylabel style={above left},                x tick label style={below},                samples = 100,                axis on top=true,                xlabel = {$x$},                 ylabel = {$f$}              ]    \addplot[very thick, domain=0:11] {5 + x};    \draw[thick, dashed] (1,6) -- ($(1,6) + (2,0)$);  \end{axis}\end{tikzpicture}\end{document}

The result is:

enter image description here

Thanks in advance!

askedNov 10, 2020 at 12:50
Allok's user avatar
3
  • The origin of the axis is not at(0,0) of the ambienttikz picture,CommentedNov 10, 2020 at 13:50
  • 3
    You have to use:\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);CommentedNov 10, 2020 at 13:50
  • 1
    I prefer to create named coordinates inside the axis environment and do all the normal TikZ stuff outside. Otherwise circles get turned into ellipses, etc.CommentedNov 10, 2020 at 15:42

2 Answers2

8

From the pgfplots documentation:

In order to express relative positions (or lengths), you need to use axis direction cs.

So you have to use:

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

enter image description here

Example:

\documentclass[dvipsnames]{article}%\usepackage[utf8]{inputenc}% need for outdated TeX distributions\usepackage{pgfplots}% loads tikz and xcolor\pgfplotsset{compat=newest}\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}\usepgfplotslibrary{fillbetween}\begin{document}\begin{tikzpicture}  \begin{axis}[ axis lines = middle,                xmin = -1,                ymin = -5,                xmax = 10,                ymax = 14,                domain = -1:10,                xtick = {1,2,...,9},                ytick = \empty,                xlabel style={below right},                ylabel style={above left},                x tick label style={below},                samples = 100,                axis on top=true,                xlabel = {$x$},                 ylabel = {$f$}              ]    \addplot[very thick, domain=0:11] {5 + x};    \draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);  \end{axis}\end{tikzpicture}\end{document}

Explanation

Inside the axis the coordinates use theaxis cs by default.

From the documentation:

The effect ofaxis cs is to apply any custom transformations (includingsymbolic x coords), logarithms, data scaling transformations or whatever pgfplots usually does and provides a low level pgf coordinate as result.

The low level pgf coordinate refers to the coordinate(rel axis cs:0,0). This is the lower left corner of the axis area (and not to the origin of the axis). Thereforeaxis cs coordinates are absolute positions in the axis. If you add them you get the unexpected result:

enter image description here

Code:

\documentclass[dvipsnames]{article}%\usepackage[utf8]{inputenc}% need for outdated TeX distributions\usepackage{pgfplots}% loads tikz and xcolor\pgfplotsset{compat=newest}\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}\usepgfplotslibrary{fillbetween}\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}\begin{document}\begin{tikzpicture}  \begin{axis}[ axis lines = middle,                xmin = -1,                ymin = -5,                xmax = 10,                ymax = 14,                domain = -1:10,                xtick = {1,2,...,9},                ytick = \empty,                xlabel style={below right},                ylabel style={above left},                x tick label style={below},                samples = 100,                axis on top=true,                xlabel = {$x$},                 ylabel = {$f$},              ]    \addplot[very thick, domain=0:11] {5 + x};    \path      (1,6) coordinate(P) node[point,label=above:P]{}      (2,0) coordinate(Q) node[point,label=above left:Q]{}      (rel axis cs:0,0) node{x}    ;    \draw[blue!50!black] (1,6) -- ($(1,6) + (2,0)$);  \end{axis}  \path (0,0) coordinate(O) node[point,label=below:O]{};% origin of the rel axis cs  \begin{scope}[->,red]    \draw (O)--(P);    \draw (O)--(Q);    \draw (Q)--+(P)node[point]{};  \end{scope}\end{tikzpicture}\end{document}

This can be fixed using either

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

or

\draw[thick,dashed] (1,6) -- ($(1,6) + (2,0)-(0,0)$);

enter image description here

Code:

\documentclass[dvipsnames]{article}%\usepackage[utf8]{inputenc}% need for outdated TeX distributions\usepackage{pgfplots}% loads tikz and xcolor\pgfplotsset{compat=newest}\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}\usepgfplotslibrary{fillbetween}\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}\begin{document}\begin{tikzpicture}  \begin{axis}[ axis lines = middle,                xmin = -1,                ymin = -5,                xmax = 10,                ymax = 14,                domain = -1:10,                xtick = {1,2,...,9},                ytick = \empty,                xlabel style={below right},                ylabel style={above left},                x tick label style={below},                samples = 100,                axis on top=true,                xlabel = {$x$},                 ylabel = {$f$},                clip=false              ]    \addplot[very thick, domain=0:11] {5 + x};    \draw[thick,blue!50!black] (1,6) -- ($(1,6) + (2,0)$);    \draw[thick,green!50!black] (1,6) -- ($(1,6) + (2,0)-(0,0)$);    \draw[thick,dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);    %    \path      (rel axis cs:0,0) coordinate(O) node[point,label=below:O]{}      (0,0) coordinate(A) node[point,label=above left:A]{}      (1,6) coordinate(P) node[point,label=above:P]{}      (2,0) coordinate(Q) node[point,label=above left:Q]{}    ;    \begin{scope}[->,red]      \draw[thick,dotted,cyan] (A)--(O);      \draw (O)--(P);      \draw (O)--(Q);      \draw (Q)--+(P)node[point]{};      \draw[thick,cyan] (Q) ++(P)-- +($(O)-(A)$)node[point]{};    \end{scope}  \end{axis}\end{tikzpicture}\end{document}
answeredNov 10, 2020 at 13:58
esdd's user avatar
8
  • I see, thanks! Could you explain, why first (1,6) and first (1,6) inside $$ environment are related to expected cs, but we have to use "axis direction cs" for (2,0)?CommentedNov 11, 2020 at 8:38
  • 1
    See the explanation in my updated answer.CommentedNov 11, 2020 at 11:48
  • Inside the axis the coordinates use the axis cs by default. axis cs coordinates are absolute positions in the axis inside axis environment (1,6) - point on the Oxy axis plane, where I want. inside axis environment (2,0) - point on the Oxy axis plane, where I want. (1,6) + (2,0) = (3,6) - that's how + works in calc. and inside axis environment (3,6) - point on the Oxy axis plane, where I want. Where am I not right? Why does + interpret first term in axis plane, but second - in rel axis cs (if I understand the explanation right)?CommentedNov 11, 2020 at 12:34
  • 1
    (1,6) is the vector from(rel axis cs:0,0) to(axis cs:1,6) and(2,0) is the vector from(rel axis cs:0,0) to(axis cs:2,0). Note that(rel axis cs:0,0) is not the origin of the axis. It differs from(axis cs:0,0). But you want to add the vector from(axis cs:0,0) to(axis cs:2,0) to the point(1,6).CommentedNov 11, 2020 at 14:03
  • 1
    Of course you could also use($(axis direction cs:1,6) + (2,0)$) or($(0,0)+(axis direction cs:1,6) + (axis direction cs:2,0)$) or($(1,6)+(2,0)-(0,0)$).CommentedNov 11, 2020 at 14:05
3

For a better idea what is going on, the coordinate (axis cs: 2,0) is the distance from (rel axis cs: 0,0). not (axis cs: 0,0). The extra 5 in the y direction comes from [ymin=-5].

\documentclass[dvipsnames]{article}\usepackage[utf8]{inputenc}\usepackage{xcolor}\usepackage{pgfplots,tikz}\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}\pgfplotsset{compat=newest}\usepgflibrary{arrows}                                                  \usepgfplotslibrary{fillbetween}\begin{document}\begin{tikzpicture}  \begin{axis}[ axis lines = middle,                xmin = -1,                ymin = -5,                xmax = 10,                ymax = 14,                domain = -1:10,                xtick = {1,2,...,9},                ytick = \empty,                xlabel style={below right},                ylabel style={above left},                x tick label style={below},                samples = 100,                axis on top=true,                xlabel = {$x$},                 ylabel = {$f$}              ]    \addplot[very thick, domain=0:11] {5 + x};    \coordinate (A) at (1,6);% axis cs: is the default    \coordinate (B) at (2,0);    \coordinate (origin) at (0,0);  \end{axis}  \draw[thick, dashed] (A) -- ($(A) + (B) - (origin)$);\end{tikzpicture}\end{document}
answeredNov 10, 2020 at 15:58
John Kormylo's user avatar
2
  • thanks! but now I'm a bit confused. I thought, that if you gonna draw plots, you'll work only inside {axis}. What's the common idea, what you will code inside axis environment, and what outside of it?CommentedNov 11, 2020 at 8:44
  • The axis cs: coordinates are only available inside the axis environment, but it also does some nasty things to TikZ commands (less now than it used to). At one point we had to use \pgfplotsextra every time we wanted to use \draw etc.CommentedNov 11, 2020 at 15:21

You mustlog 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.