簡體   English   中英

使用 Tikz 精確平滑的曲線

[英]Precise and smooth curve with 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或任何其他命令?

使用所有這些點(除了最后一個)具有零梯度的附加信息,貝塞爾曲線似乎比平滑圖更適合。

所以,你可能會實現這種事情......

結果

用這段代碼...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM