我正在尝试绘制类似的箭头线(A) -> (A1) -> (A2) -> (A3)
,但我的代码不起作用。
\documentclass[11pt,a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{pgfplots, relsize}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\usetikzlibrary{arrows,decorations.markings}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
unit vector ratio*=1 1 1,
grid = none,
axis x line = middle,
axis y line = middle,
xlabel={$k_t$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$k_{t+1}$},
ylabel style={at=(current axis.above origin), anchor=south},
xmin = 0,
xmax = 15,
scale=1.5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
ymin = 0,
ymax = 10, xtick=\empty,ytick=\empty,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west] {0};},
]
\coordinate (0,0) ;
% domain can be set individually!!
\addplot[color=red, domain=0:10,samples=100,smooth,thick] {x} node[above,pos=1] {$45^\circ$};
\addplot[color=blue,domain=0:11,samples=1000,smooth, thick] {3*sqrt(x)} node[right,pos=1] {$W(s,k_{t},\delta)$};
\addplot[color=Cerulean,domain=0:11,samples=1000,smooth, thick] {2*sqrt(x)} node[right,pos=1] {$W(s',k_{t},\delta)$};
\addplot[mark=*,only marks, fill=white] coordinates {(9,9)} node[above, pos=1]{$A$};
\addplot[mark=*,only marks, fill=white] coordinates {(4,4)} node[above, pos=1]{$A'$};
\addplot[mark=*,only marks, fill=white] coordinates {(9,6)} node[below, pos=1]{$A_1$};
\addplot[mark=*,only marks, fill=white] coordinates {(6,6)} node[above, pos=1]{$A_2$};
\addplot[mark=*,only marks, fill=white] coordinates {(6,4.899)} node[below, pos=1]{$A_3$};
\addplot[mark=*,only marks, fill=white] coordinates {(4.899,4.899)} node[above, pos=1]{$A_4$};
% NO output shown at all
\node (A) at (9, 9){};
\node (Ap) at (4, 4){};
\node (A1) at (9, 6){};
\node (A2) at (6, 6){};
\node (A3) at (6, 4.899){};
\node (A4) at (4.899, 4.899){};
\draw [dashed] (A) -- (A1) -- (A2) -- (A3) -- (A4);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
输出严重不成比例(如果您找不到它,它是原点附近的小虚线)。位置和比例完全偏离。有趣的是,当我将声明\node
和\draw
语句放在外面时{axis}
,输出再次偏离,但偏离的方向相反。我相信我没有\node
正确定义与{axis}
坐标相关的对象,它们都位于其他坐标系中。
有办法解决这个问题吗?谢谢!
答案1
这仅是对多个箭头部分的回答。Stefan Pinnow 已经指出,应该使用较新版本的 pgfplots 兼容性。在这里,我展示了如何通过 (ab) 使用 来绘制多个箭头show path construction
。
\documentclass[11pt,a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{pgfplots, relsize}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows,decorations.markings,decorations.pathreplacing}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
unit vector ratio*=1 1 1,
grid = none,
axis x line = middle,
axis y line = middle,
xlabel={$k_t$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$k_{t+1}$},
ylabel style={at=(current axis.above origin), anchor=south},
xmin = 0,
xmax = 15,
scale=1.5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
ymin = 0,
ymax = 10, xtick=\empty,ytick=\empty,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west] {0};},
]
\coordinate (0,0) ;
% domain can be set individually!!
\addplot[color=red, domain=0:10,samples=100,smooth,thick] {x} node[above,pos=1] {$45^\circ$};
\addplot[color=blue,domain=0:11,samples=1000,smooth, thick] {3*sqrt(x)} node[right,pos=1] {$W(s,k_{t},\delta)$};
\addplot[color=cyan,domain=0:11,samples=1000,smooth, thick] {2*sqrt(x)} node[right,pos=1] {$W(s',k_{t},\delta)$};
\addplot[mark=*,only marks, fill=white] coordinates {(9,9)} node[above, pos=1]{$A$};
\addplot[mark=*,only marks, fill=white] coordinates {(4,4)} node[above, pos=1]{$A'$};
\addplot[mark=*,only marks, fill=white] coordinates {(9,6)} node[below, pos=1]{$A_1$};
\addplot[mark=*,only marks, fill=white] coordinates {(6,6)} node[above, pos=1]{$A_2$};
\addplot[mark=*,only marks, fill=white] coordinates {(6,4.899)} node[below, pos=1]{$A_3$};
\addplot[mark=*,only marks, fill=white] coordinates {(4.899,4.899)} node[above, pos=1]{$A_4$};
% NO output shown at all
\node (A) at (9, 9){};
\node (Ap) at (4, 4){};
\node (A1) at (9, 6){};
\node (A2) at (6, 6){};
\node (A3) at (6, 4.899){};
\node (A4) at (4.899, 4.899){};
\draw [decorate,decoration={show path construction,
lineto code={\draw[dashed,-latex] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast); }}]
(A) -- (A1) -- (A2) -- (A3) -- (A4);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案2
正如前面提到的在问题下方评论节点“位移”是因为您(意外地)为 TikZ 坐标使用了错误的坐标系。为了避免这种情况,可以\pgfplotsset{compat=1.11}
在前言中声明(或更高的值),或者在前面加上 TikZ 坐标,axis cs:
否则将使用tikzpicture
坐标而不是坐标axis
。
这里我提出了一个“干净”的尝试来实现你想要的,而不需要重复,而且这也不需要上述的东西。正如 marmot 已经在他在这个问题下的评论你可以利用edge
画出带箭头的连接线,但我借用decoration
了他的回答这好多了。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.pathreplacing}
\pgfplotsset{
% declare the style of the marks/nodes here (once)
points/.style={
mark=*,
only marks,
fill=white,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel={$k_t$},
xlabel style={
at=(current axis.right of origin),
anchor=west,
},
ylabel={$k_{t+1}$},
ylabel style={
at=(current axis.above origin),
anchor=south,
},
xmin=0,
xmax=15,
scale=1.5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
ymin=0,
ymax=10,
xtick=\empty,
ytick=\empty,
after end axis/.code={
\path (0,0) node [anchor=north west] {0};
},
% ---------------------------------------------------------------------
% moved common `\addplot' options here
smooth,
% there is no need to use that many coordinates
% (to further decrease them you can adapt use "non-linear spacing"
% between the points as e.g. shown at
% <https://tex.stackexchange.com/a/373820/95441>)
samples=201,
domain=0:11,
]
\addplot [red,thick,domain=0:10,samples=2] {x}
node [above] {$45^\circ$}
;
\addplot [blue,thick] {3*sqrt(x)}
node [right] {$W(s,k_{t},\delta)$}
;
\addplot [green,thick] {2*sqrt(x)}
node [right] {$W(s',k_{t},\delta)$}
;
\addplot [
points,
% specify the points where the points should be shown ...
samples at={4,4.899,6,9},
] {x}
% ... and add nodes with labels accordingly
node [pos=0.00,label=$A'$] (Ap) {}
node [pos=0.25,label=$A_4$] (A4) {}
node [pos=0.50,label=$A_2$] (A2) {}
node [pos=1.00,label=$A$] (A) {}
;
\addplot [
points,
samples at={6,9},
] {2*sqrt(x)}
node [pos=0,label=below:$A_3$] (A3) {}
node [pos=1,label=below:$A_1$] (A1) {}
;
% draw the connecting line between the points
\draw [
decorate,
decoration={
show path construction,
lineto code={
\draw [dashed,-latex]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast)
;
}
},
] (A) -- (A1) -- (A2) -- (A3) -- (A4);
\end{axis}
\end{tikzpicture}
\end{document}
答案3
您必须使用axis cs
坐标系来指定 pgfplots 中轴内的点。还有其他几个坐标系可供使用(请参阅手册),但就您而言,这是您需要的坐标系。
因此,您的代码变为(我省略了未更改的部分)
\node (A) at (axis cs:9, 9){};
\node (Ap) at (axis cs:4, 4){};
\node (A1) at (axis cs:9, 6){};
\node (A2) at (axis cs:6, 6){};
\node (A3) at (axis cs:6, 4.899){};
\node (A4) at (axis cs:4.899, 4.899){};
\draw [dashed] (A) -- (A1) -- (A2) -- (A3) -- (A4);
为了改变使用的坐标系,必须在第一个坐标规范前面加上相应的名称和冒号,就像我在给定的例子中所做的那样。