![如何装饰同一张图中的两条路径?](https://linux22.com/image/426854/%E5%A6%82%E4%BD%95%E8%A3%85%E9%A5%B0%E5%90%8C%E4%B8%80%E5%BC%A0%E5%9B%BE%E4%B8%AD%E7%9A%84%E4%B8%A4%E6%9D%A1%E8%B7%AF%E5%BE%84%EF%BC%9F.png)
有一张由两个 addplot 命令组成的图
但箭头必须指向同一方向(如上图所示),轴消失了(或变得透明)。这两个部分是两个独立的功能,而不是一个(这就是箭头不指向同一方向的原因)。那么如何:
- 箭头是否可以指向同一个方向(调整装饰部分或使抛物线连续运行)
- 如何让轴线显现出来?
代入https://pastebin.com/W77g6Wtj,这里的代码选项当时对我来说不起作用。
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{flushleft}
\begin{tikzpicture}[scale=0.7,decoration={markings,
mark=at position 0.5cm with {\arrow[line width=1pt]{>}},
mark=at position 2cm with {\arrow[line width=1pt]{>}},
mark=at position 7.85cm with {\arrow[line width=1pt]{>}},
mark=at position 9cm with {\arrow[line width=1pt]{>}}
}]
\begin{axis}[
axis lines=middle,
axis equal,
samples = 200,
xlabel = {$x$},
ylabel = {$y$},
xmin=-4,xmax=4,ymin=-5,ymax=5,
]
\addplot[red,postaction=decorate]{sqrt(x+1)+2} node[pos=1,below]{$ $};
\addplot[red,postaction=decorate]{-1*(sqrt(x+1)-2)} node[pos=1,below]{$ $};
\draw[fill=red] (-1,2) circle (0.2);
\draw[dashed,color=blue] (axis cs:0,2) -- (axis cs:-1,2);
\end{axis}
\end{tikzpicture}
\end{flushleft}
\end{document}
答案1
使用pgfplots
参数函数:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
bending,
decorations.markings}
\begin{document}
\begin{tikzpicture}[
> = {Straight Barb[scale=0.8, bend]},
->-/.style = {decoration={markings,% switch on markings
mark=between positions 0.1 and 0.9 step 0.25
with {\arrow[line width=1pt]{>}}
},
postaction={decorate}, draw=red, thick
},
lbl/.style = {font=\scriptsize, text=black, inner sep=1pt,
near end, sloped, above}
]%
\begin{axis}[
declare function = {f(\t)=((\t-2)^2-1);}, % (x,sqrt(x)) -> (x^2,x)
axis lines=middle,
axis equal,
xlabel = {$x$},
ylabel = {$y$},
xmin=-2.5, xmax=4.5,
ymin=-2.5, ymax=4.5,
ticklabel style={font=\footnotesize},
domain=-0.5:4.5, samples = 200,
]
\addplot [->-] ({f(x)},x)
node[lbl]{$\sqrt{(x+1)}+2$};
\fill[red] (-1,2) circle[radius=2pt];
\draw[densely dashed,blue] (0,2) -- (-1,2);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
plot
我建议只使用路径操作而不是 的解决方案addplot
。您可以对轴进行更多操作,但从装饰的角度来看,这可能更容易。
- 即使你回去
pgfplots
,也画一条参数曲线而不是两个图形。 - 将装饰包含在 中
postaction
。顺便说一句,对代码的最小修正是定义两个装饰,并为每个分支使用一个。第二个必须将箭头反转,{<}
。
代码
\documentclass[11pt, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\tikzset{
flow/.style={%
decorate,
decoration={markings,
mark=between positions .1 and .9 step{#1} with {%
\arrow[line width=1pt]{>}}
}
}
}
\begin{tikzpicture}[every node/.style={scale=.8}]
\draw[->] (-2.2, 0) -- (6.5, 0) node[above right] {$x$};
\draw[->] (0,-4.2) -- (0, 4.5) node[above left] {$y$};
\foreach \i in {-2, 2, 4, 6}{%
\draw (\i, 2pt) -- ++(0, -4pt) node[below] {$\i$};
}
\foreach \i in {-4, -2, 2, 4}{%
\draw (2pt, \i) -- ++(-3pt, 0) node[left] {$\i$};
}
\draw[red, thick, postaction={flow={.265}}]
plot[domain=-.6:4.6] ({(\x-2)*(\x-2)-1}, \x-.1);
\end{tikzpicture}
\end{document}