在我的示例中,我有两条简单曲线 y = sqrt(x) 和 y = x。我希望能够沿着每条曲线附加一个标签,比如在曲线中间。现在,我正在反复试验,直到看起来“正确”。我如何使用节点定位来实现我所拥有的?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
\begin{scope}
\draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
\draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
\draw[-,domain=0:1,samples=250] plot (\x,{sqrt(\x)});
\draw[-,domain=0:1,samples=250] plot (\x,{\x});
\node at (0.35,0.75) {\footnotesize $y=\sqrt{x}$};
\node at (0.65,0.5) {\footnotesize $y=x$};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
这是一个mylabel
定义样式的解决方案,它需要 3 个参数{at #1 #2 with #3}
,这些参数被转换成`在 #1(位置)#2(上/下/左/右)和 #3(标签)
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\tikzset{mylabel/.style args={at #1 #2 with #3}{
postaction={decorate,
decoration={
markings,
mark= at position #1
with \node [#2] {#3};
} } } }
\begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
\begin{scope}[domain=0:1]
\draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
\draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
\draw[-,domain=0:1,samples=250,mylabel=at 0.7 above left with {$y=\sqrt{x}$}] plot (\x,{sqrt(\x)});
\draw[-,domain=0:1,samples=250,mylabel=at 0.5 below right with {$y=x$}] plot (\x,{\x});
% \node at (0.35,0.75) {\footnotesize $y=\sqrt{x}$};
% \node at (0.65,0.5) {\footnotesize $y=x$};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
对于图表,我建议您使用axis
环境,pgfplots
并使用以下方式放置图表标签,并nodes
指定沿线的位置pos=
:
代码:
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmax = 1.1,
ymax = 1.1,
ylabel=$y$,
xlabel=$x$,
]
\addplot [domain=0:1,samples=250, ultra thick, blue] {sqrt(x)}
node [pos=0.9, above left] {$y=\sqrt{x}$};
\addplot [domain=0:1,samples=250, ultra thick, red ] {x}
node [pos=0.3, below right] {$y=x$};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
仅用于使用 PSTricks 进行打字练习。
选项1
\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\psset{unit=4cm}
\def\f{sqrt(x)}
\def\g{x}
\begin{document}
\multido{\r=0.0+0.1}{15}{%
\begin{pspicture}[algebraic,plotpoints=200](-1,-1)(2.25,2.25)
\psaxes{->}(0,0)(-1,-1)(2,2)[$x$,0][$y$,90]
\psplot[linecolor=red]{0}{1.5}{\f}
\psplot[linecolor=blue]{-.5}{1.5}{\g}
\uput[45](*1.5 {\g}){\textcolor{blue}{$y=x$}}
\uput[90](*\r\space {\f}){\textcolor{red}{$y=\sqrt x$}}
\end{pspicture}}
\end{document}
选项 2
\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot,pst-text}
\psset{unit=4cm}
\def\f{sqrt(x)}
\def\g{x}
\begin{document}
\multido{\r=-1.0+0.1}{18}{%
\begin{pspicture}[algebraic,plotpoints=200](-1,-1)(2.25,2.25)
\psaxes{->}(0,0)(-1,-1)(2,2)[$x$,0][$y$,90]%
\pstextpath[r](0,.1){\psplot[linecolor=blue]{-.5}{1.5}{\g}}{\textcolor{blue}{$g(x)= x$}}%
\pstextpath[c](\r,.1){\psplot[linecolor=red]{0}{1.5}{\f}}{\textcolor{red}{$f(x)=\sqrt x$}}%
\end{pspicture}}
\end{document}
笔记:红色的小横线是额外赠送的。这是 bug 吗?我不知道!
答案4
您是否满意:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
\begin{scope}
\draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
\draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
\draw[-,domain=0:1,samples=250] plot (\x,{sqrt(\x)});
\draw[-,domain=0:1,samples=250] plot (\x,{\x});
\node[above left] at (0.5,{sqrt(0.5)}) {\footnotesize $y=\sqrt{x}$};
\node[below right] at (0.5,0.5) {\footnotesize $y=x$};
\end{scope}
\end{tikzpicture}
\end{document}
我只是通过以下方式定位节点:
at (X,{F(X)})
我在这里选择了域中间的 X。并且我指定文本应该位于上方/下方、左侧/右侧...