
次のコードではなぜ傾斜した線が描画されるのでしょうか? 合計 ($(1,6) + (2,0)$) は点 (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}
結果は次のとおりです。
前もって感謝します!
答え1
pgfplots ドキュメントより:
相対的な位置(または長さ)を表現するには、軸方向 cs を使用する必要があります。
したがって、以下を使用する必要があります。
\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
例:
\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}
説明
軸の内側では、座標はaxis cs
デフォルトで を使用します。
ドキュメントより:
の効果は、
axis cs
カスタム変換 (を含む)、対数、データ スケーリング変換、または pgfplots が通常実行するあらゆる変換を適用しsymbolic x coords
、結果として低レベルの pgf 座標を提供することです。
低レベルの pgf 座標は、座標 を参照します(rel axis cs:0,0)
。これは、軸領域の左下隅です (軸の原点ではありません)。したがって、axis cs
座標は軸内の絶対位置です。これらを追加すると、予期しない結果になります。
コード:
\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}
これは、次のいずれかを使用して修正できます。
\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
または
\draw[thick,dashed] (1,6) -- ($(1,6) + (2,0)-(0,0)$);
コード:
\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}
答え2
何が起こっているのかもっとよく理解するために、座標 (axis cs: 2,0) は (rel axis cs: 0,0) からの距離です。 (axis cs: 0,0) ではありません。y 方向の余分な 5 は [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}