是否可以简化代码,也就是说不要使用三次命令 \addplot[] ?
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
restrict x to domain=-5:9, xmax=9, xmin=-5,
restrict y to domain=-3:4, ymax=4, ymin=-3,
x=1cm,
y=1cm,
axis x line = middle,
axis y line = middle,
axis line style =ultra thick,
major tick style=black,
grid=both,
major grid style=lightgray,
minor grid style=lightgray,
minor tick num=1,
xtick={-5,...,8},
ytick={-3,...,3},
samples=1000,
>=stealth,
]
\addplot[
patch,
red,
patch type=quadratic spline,
thick,
]
coordinates{
(-4,0) (0,1)(-2,-3) (-0.6,0)
};
\addplot[
patch,
blue,
patch type=quadratic spline,
thick,
]
coordinates{
(0,1) (2,1)(1,2)(1.4,1.8)
};
\addplot[
patch,
green,
patch type=cubic spline,
thick,
]
coordinates{
(2,1) (7,0)(3,0.4)(5,0.1)
};
\node[fill=black,circle,scale=0.4] at (-4,0){};
\node[fill=black,circle,scale=0.4] at (7,0){};
\node[below left] at (axis cs:0,0) {$0$};
\node[below] at (axis cs:9.8,-0.1) {$x$};
\node[left] at (axis cs:-0.1,3.8) {$y$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
据我所知,您希望生成一个“接近”屏幕截图的图,而无需 100% 的准确度。这听起来很像“使其平滑并确保它在某些关键点进行插值”。
虽然这可以使用手动计算的样条线来完成,但它很麻烦,因为您需要摆弄交叉点处的角度。
还有一个自动解决方案,它由两个步骤组成:首先生成图(在整个域上,这意味着您可以使用 tikz 的smooth
er),其次计算交叉点段并以所需的颜色绘制它们。整个方法的工作原理如下:
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmax=9, xmin=-5,
ymax=4, ymin=-3,
x=1cm,
y=1cm,
axis x line = middle,
axis y line = middle,
axis line style =ultra thick,
major tick style=black,
grid=both,
major grid style=lightgray,
minor grid style=lightgray,
minor tick num=1,
xtick={-5,...,8},
ytick={-3,...,3},
>=stealth,
clip=false,% <--- otherwise $x$ will be clipped
]
\addplot[smooth,
draw=none,% <- this plot is INVISIBLE
tension=0.6,
name path=plot]
coordinates{
(-4,0) (-2,-3) %(-0.6,0)
(0,1) (1,2) %(1.4,1.8)
(2,1)(3,0.4)
(5,0.1) (7,0)
};
\path[name path=cut line] (-4,1) -- (10,1);
\draw[red,ultra thick,
intersection segments={of=plot and cut line,sequence=L1}];
\draw[blue,ultra thick,
intersection segments={of=plot and cut line,sequence=L2}];
\draw[green,ultra thick,
intersection segments={of=plot and cut line,sequence=L3}];
\node[fill=black,circle,scale=0.4] at (-4,0){};
\node[fill=black,circle,scale=0.4] at (7,0){};
\node[below left] at (0,0) {$0$};
\node[below] at (9.8,-0.1) {$x$};
\node[left] at (-0.1,3.8) {$y$};
\end{axis}
\end{tikzpicture}
\end{document}
您会看到我将插值点组合成一个\addplot
。该图使用 tikz 的图处理程序,可实现平滑过渡并使用某些参数smooth
控制自由度。具有,即仅记住,tension
\addplot
draw=none, name path=plot
不是画。
然后我们有\path[name path=cut line] (-4,1) -- (10,1);
定义和名称cut line
。
最后,有三条\draw
指令定义特定 的颜色intersection segments
。该指令属于fillbetween
附带的库pgfplots
。该参数of=plot and cut line
计算这两个图的交点,并sequence
允许选择结果中的单个项:“L”是 中左参数的第项of=left and right
,“R”是 中右参数的第项(此处未使用)。
答案2
出于完整性考虑,以下是您可以使用 执行的操作to[in=<angle>, out=<angle>, looseness=<value>]
。主要缺点是您必须猜测并改进值,因此您可能需要进行几次迭代,直到结果令人满意。
代码
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[ xmin=-4.5,
xmax=7.5,
ymin=-3.5,
ymax=2.5,
x=1cm,
y=1cm,
domain=-4:7,
axis x line = middle,
axis y line = middle,
axis line style =ultra thick,
major tick style=black,
grid=both,
major grid style=lightgray,
minor grid style=lightgray,
minor tick num=1,
xtick={-4,...,7},
ytick={-3,...,2},
samples=100,
>=stealth,
]
\draw[thick, red]
(-4,0) to[out=-80, in=180, looseness=0.6]
(-2,-3) to[out=0, in=250, looseness=0.4]
(-0.5,0) to[out=70, in=240, looseness=0.8]
(0,1) to[out=60, in=180, looseness=0.7]
(1,2) to[out=0, in=120, looseness=0.8]
(2,1) to[out=300, in=180, looseness=0.5]
(7,0);
\node[fill=black,circle,scale=0.4] at (-4,0){};
\node[fill=black,circle,scale=0.4] at (7,0){};
\node[below left] at (0,0) {$0$};
\node[above left] at (7.5,0) {$x$};
\node[below right] at (0,2.5) {$y$};
\end{axis}
\end{tikzpicture}
\end{document}
输出
答案3
使用 TikZ 的解决方案..controls
:
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\draw[step=1.0,lightgray,thin] (-6,-4) grid (9,4);
\draw[ultra thick,->,>=stealth](-6,0)--(9,0);
\draw[ultra thick,->,>=stealth](0,-4)--(0,4);
\draw [thick,red](-4,0)..controls(-2,-4)..(-0.5,0)..controls(0,1)..(0,1)..controls(1,2.31)..(2,1)..controls(3.1,0.2)..(7,0);
\draw [thick](1,0.1)--(1,-0.1);
\draw [thick](-0.1,1)--(0.1,1);
\node[below] at (1,0) {$1$};
\node[left] at (0,1) {$1$};
\node[below left] at (0,0) {$0$};
\node[below] at (8.8,-0.1) {$x$};
\node[left] at (-0.1,3.8) {$y$};
\node[fill=black,circle,scale=0.4] at (-4,0){};
\node[fill=black,circle,scale=0.4] at (7,0){};
\end{tikzpicture}
\end{document}