答案1
这种类型的草图可以通过\draw
使用 来完成controls
。第一个示例代码(我使用网格来定位控制点):
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[ultra thin, gray!30](0,0) grid (5,5);
\draw[thick](0,5) |- (5,0);
\draw[thick,red](0,0.5) -- (1,0.5) ..controls(1.75,0.5) and (1.25,4).. (2,4) ..controls(2.75,4) and (2.25,0.5).. (3,0.5) -- (5,0.5);
\end{tikzpicture}
\hspace{5mm}
\begin{tikzpicture}
\draw[ultra thin, gray!30](0,0) grid (5,5);
\draw[thick](0,5) |- (5,0);
\draw[thick,green](0,0.5) -- (1,0.5) ..controls(2.5,0.5) and (1.5,4).. (3,4) -- (5,4);
\end{tikzpicture}
\hspace{5mm}
\begin{tikzpicture}
\draw[ultra thin, gray!30](0,0) grid (5,5);
\draw[thick](0,5) |- (5,0);
\draw[thick,blue](0,0.5) -- (0.5,0.5)
..controls (1.5,0.5) and (1.25,4).. (1.5,4)
..controls (1.75,4) and (1.75,0.5).. (2,0.5)
..controls (2.25,0.5) and (2.25,4).. (2.5,4)
..controls (2.75,4) and (2.75,0.5).. (3,0.5)
..controls (3.25,0.5) and (3.25,4).. (3.5,4)
..controls (3.75,4) and (3.5,0.5).. (4.5,0.5) -- (5,0.5);
\end{tikzpicture}
\end{document}
这里最重要的部分是如何controls
工作。在下面的例子中,曲线从 开始,A
方向朝向B
,并结束于D
,方向从 开始C
。使用这样的组件,您可以制作最常见的基本函数。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[ultra thin,gray!30](0,0) grid (5,5);
\draw[very thick,blue!30] (0,1) coordinate(start) --
(1,1) coordinate(A) --
(2.5,1) coordinate(B) --
(2.5,4) coordinate(C) --
(4,4) coordinate(D) --
(5,4) coordinate(end);
\foreach \Point in {start,A,B,C,D,end}{
\fill[blue] (\Point) circle(2pt);
\node[above] at (\Point) {\Point};
}
\draw[red] (start) -- (A) ..controls(B) and (C).. (D) -- (end);
\end{tikzpicture}
\end{document}
答案2
pics
您可以为此定义小助手( )。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/graph/.style={code={
\draw[thick,pic actions] plot[variable=\x,smooth,domain=0:1] (\x,{#1});
\draw[thick,black] (0,1) |- (1,0);
}}]
\path[scale=4,transform shape]
(0,3) pic[red]{graph={0.8*exp(-20*(\x-0.4)*(\x-0.4)))}}
(0,1.5) pic[green!60!black]{graph={0.5*(1+tanh(8*(\x-0.4)))}}
(0,0) pic[blue]{graph={0.8*exp(-100*(\x-0.2)*(\x-0.2)))+
0.8*exp(-100*(\x-0.5)*(\x-0.5)))+0.8*exp(-100*(\x-0.8)*(\x-0.8)))}};
\end{tikzpicture}
\end{document}