首页 > 解决方案 > 使用 Tikz 实现精确平滑的曲线

问题描述

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

我需要的

我得到了什么

\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或任何其他命令?

标签: tikzpgf

解决方案


使用所有这些点(最后一个点除外)都具有零梯度的附加信息,贝塞尔曲线似乎比平滑图更适合。

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

结果

用这段代码...

\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}

推荐阅读