
Por que o código a seguir desenha uma linha inclinada? Achei que a soma ($(1,6) + (2,0)$) retornaria um ponto (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}
O resultado é:
Desde já, obrigado!
Responder1
Da documentação do pgfplots:
Para expressar posições relativas (ou comprimentos), você precisa usar a direção do eixo cs.
Então você tem que usar:
\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
Exemplo:
\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}
Explicação
Dentro do eixo as coordenadas usam axis cs
por padrão.
Da documentação:
O efeito
axis cs
é aplicar quaisquer transformações personalizadas (incluindosymbolic x coords
), logaritmos, transformações de escalonamento de dados ou o que quer que o pgfplots normalmente faça e forneça uma coordenada pgf de baixo nível como resultado.
A coordenada pgf de baixo nível refere-se à coordenada (rel axis cs:0,0)
. Este é o canto inferior esquerdo da área do eixo (e não a origem do eixo). Portanto, axis cs
as coordenadas são posições absolutas no eixo. Se você adicioná-los, obterá o resultado inesperado:
Código:
\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}
Isso pode ser corrigido usando
\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
ou
\draw[thick,dashed] (1,6) -- ($(1,6) + (2,0)-(0,0)$);
Código:
\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}
Responder2
Para ter uma ideia melhor do que está acontecendo, a coordenada (eixo cs: 2,0) é a distância de (eixo rel cs: 0,0). não (eixo cs: 0,0). O 5 extra na direção y vem de [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}