gpt4 book ai didi

tikz - 使用 Tikz 精确平滑的曲线

转载 作者:行者123 更新时间:2023-12-04 14:16:21 29 4
gpt4 key购买 nike

我正在尝试绘制一些曲线,但效果不是很好。

  • 我需要什么

what I need

  • 我得到了什么

what I get

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ticks=none,
xtick distance=1,
ytick distance=1,
axis equal image=true,
grid,
grid style={gray!50},
grid=both,
xlabel={$x$},
ylabel={$y$},
axis lines=middle,
xmin=-4, xmax=9, ymin=-5, ymax=4,
axis x line=center,
axis y line=center,
]
\addplot[thick, smooth] plot coordinates
{
(-3, -1)
(-.5, -3)
(.5, -1.9)
(1.5, -2.8)
(3.5, 1)
(5.5, 3)
(7.5, -1.95)
(8, -1.5)
};
\end{axis}
\end{tikzpicture}
\end{document}

是否可以在不添加大量点的情况下构建如此平滑的曲线?在原图中,您可以看到几个引用点。有什么方法可以配置 \addplot 或任何其他命令吗?

最佳答案

使用所有这些点(最后一个点除外)的梯度为零的附加信息,Bézier curves似乎比平滑的情节更适合。

所以,你可能会实现这种事情......

Result

用这段代码...

\documentclass[border=2pt]{standalone} 

\usepackage{fourier}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[thick, line cap=round, >=latex, scale=0.5]

% Some constants for the Bezier curve
\def\x{0.5}
\def\u{1.0}
\def\v{0.3}
\def\w{0.1}

% The grid, axes and labels
\draw[thin, gray!20, help lines] (-4.5 ,-4.5) grid (9.5 ,4.5);
\draw[very thick, ->] (-4.5, 0) -- (9.8, 0) node[above] {$x$};
\draw[very thick, ->] (0, -4.5) -- (0, 4.8) node[left] {$y$};
\path (-3, 0) coordinate (x1) node[above] {$-3$}
(0, 0) coordinate (x2) node[below] {$\quad0$}
(1, 0) coordinate (x3)
(8, 0) coordinate (x4) node[below] {$8$}
(0, 1) coordinate (y1) node[right] {$1$};

% ticks are really cumbersome
\draw[very thick] ($(x1)+(0,-\w)$) -- ($(x1)+(0,\w)$);
\draw[very thick] ($(x3)+(0,-\w)$) -- ($(x3)+(0,\w)$);
\draw[very thick] ($(x4)+(0,-\w)$) -- ($(x4)+(0,\w)$);
\draw[very thick] ($(y1)+(-\w,0)$) -- ($(y1)+(\w,0)$);

% Finally, the points...
\path (-3, -1) coordinate (A)
(-.5,-3) coordinate (B)
(.5, -1.9) coordinate (C)
(1.5,-2.8) coordinate (D)
(3.5, 1) coordinate (E)
(5.5, 3) coordinate (F) node[above] {$y=F(x)$}
(7.5,-1.95) coordinate (G)
(8, -1.5) coordinate (H);

% and the line with suitable gradients (after a bit trial and error)
\draw[ultra thick]
(A) ..controls +(\u, 0) and ( $(B) + (-\x, 0)$ )..
(B) ..controls +(\x, 0) and ( $(C) + (-\v, 0)$ )..
(C) ..controls +(\v, 0) and ( $(D) + (-\v, 0)$ )..
(D) ..controls +(\x, 0) and ( $(E) + (-\u, 0)$ )..
(E) ..controls +(\u, 0) and ( $(F) + (-\x, 0)$ )..
(F) ..controls +(\x, 0) and ( $(G) + (-\u, 0)$ )..
(G) ..controls +(\w, 0) and ( $(H) + (-\w,-\v)$ )..
(H);

% two small circles to mark the curve ends
\draw[thin, fill=white] (A) circle (3pt);
\draw[thin, fill=white] (H) circle (3pt);
\end{tikzpicture}
\end{document}

关于tikz - 使用 Tikz 精确平滑的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59880248/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com