TikZ/pgfplots:如何產生這樣的圖形?

TikZ/pgfplots:如何產生這樣的圖形?

製作這樣的人物最簡單的方法是什麼?曲線應該看起來是擺動和隨機的,並且總是不增加,除了向上的跳躍。我嘗試過使用隨機步驟的裝飾,但這看起來不太正確,有時會增加。我還嘗試了一個包含許多隨機生成的單獨座標的圖,但看起來不夠平滑。

我很樂意使用普通的 TikZ 或 pgfplots。

在此輸入影像描述

編輯:這裡有兩個嘗試。

第一個使用了random steps裝飾,但情節並不順利,有時會增加。我嘗試了幾種不同的幅度。

\documentclass{standalone}

\usepackage{tikz}
\pgfplotsset{compat=1.10}

\begin{document}

    \begin{tikzpicture}

        \draw[->] (0,0) -- (10,0);
        \draw[->] (0,0) -- (0,5);

        \draw [decorate, decoration={random steps,amplitude=2pt}] (0.2,4) -- (3,1);
        \draw (3,1) -- (3,5);
        \draw [decorate, decoration={random steps,amplitude=5pt}] (3,5) -- (5,0.2);
        \draw (5,0.2) -- (5,3);
        \draw [decorate, decoration={random steps,amplitude=8pt}] (5,3) -- (8,1.5);
        \draw (8,1.5) -- (8,4);
        \draw [decorate, decoration={random steps,amplitude=5pt}] (8,4) -- (9,3.5);

        \useasboundingbox (-1,-1) rectangle (11,6);

    \end{tikzpicture}

\end{document}  

在此輸入影像描述

第二次嘗試使用帶有大量精細間隔座標的 pgfplots(我在 Excel 中隨機產生的)。這個比較接近,但是粒度太細,不夠平滑。

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

    \begin{tikzpicture}

        \begin{axis} [
            axis lines=left,
            xtick=\empty,
            ytick=\empty,
        ]

        \addplot [mark size=0]
            coordinates {
                (0.2,4)
                (0.245550438070978,3.9189299356319)
                (0.309894093387146,3.8555584914932)
                (0.374626991695131,3.77679077960278)
                (0.380585874068229,3.74823005668191)
                ... you get the idea ...                
                (11.2737449020538,2.23155401800146)
                (11.2994722948852,2.22522905911657)
                (11.3669785475168,2.17668213475497)
        };

        \end{axis}

    \end{tikzpicture}

\end{document}  

在此輸入影像描述

答案1

首先我們來看看裝潢random step

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\makeatletter



\tikzset{
    demo decoration/.style={
        gray,
        postaction={draw=red,decorate,decoration={segment length=6pt,amplitude=3pt,meta-amplitude=12pt,#1}}
    }
}
\begin{tikzpicture}[remember picture]
    \path(0,0)node(A){}(6,0)node(B){};
    \draw[demo decoration=random steps](A)to[bend left](B);
\end{tikzpicture}

我可以平滑它由\pgfsetcornersarced.所以我創建了一個名為 的裝飾random drift

\pgfdeclaredecoration{random drift}{start}
{
  \state{start}[width=+0pt,next state=step,persistent precomputation=\pgfdecoratepathhascornerstrue]
  {
    \egroup
    \pgfsetcornersarced{\pgfqpoint{.2\pgfdecorationsegmentlength}{.2\pgfdecorationsegmentlength}}
    \bgroup
  }
  \state{step}[width=+\pgfdecorationsegmentlength]
  {
    \pgfpathlineto{
      \pgfpointadd
      {\pgfqpoint{\pgfdecorationsegmentlength}{0pt}}
      {\pgfpoint{rand*\pgfdecorationsegmentamplitude}{rand*\pgfdecorationsegmentamplitude}}
    }
  }
  \state{final}
  {}
}
\begin{tikzpicture}
    \draw[demo decoration=random drift](A)to[bend left](B);
\end{tikzpicture}

然後我添加一些跳躍。這裡我用來\pgf@randomsaw@y儲存 y 座標並為其添加負隨機長度,因此函數是不遞增的。

\pgfdeclaredecoration{random saw}{start}
{
  \state{start}[width=+0pt,next state=step,persistent precomputation=\pgfdecoratepathhascornerstrue]
  {
    \egroup
    \pgfsetcornersarced{\pgfqpoint{.2\pgfdecorationsegmentlength}{.2\pgfdecorationsegmentlength}}
    \bgroup
    \newdimen\pgf@randomsaw@y
  }
  \state{step}[width=+\pgfdecorationsegmentlength]
  {
    \pgfmathsetlength\pgf@ya{-rnd*\pgfmetadecorationsegmentamplitude}
    \pgfmathsetlength\pgf@yb{ rnd*\pgfmetadecorationsegmentamplitude}
    \ifdim\pgf@randomsaw@y<\pgf@ya
      \pgfsetcornersarced{\pgfqpoint{0pt}{0pt}}
      \pgfpathlineto{\pgfpoint{\pgfdecorationsegmentlength}{\pgf@randomsaw@y-4*rnd*\pgfdecorationsegmentamplitude}}
      \pgfpathlineto{\pgfqpoint{\pgfdecorationsegmentlength}{\pgf@yb}}
      \global\pgf@randomsaw@y\pgf@yb
    \else
      \pgfmathsetlength\pgf@xa{\pgfdecorationsegmentlength+rand*\pgfdecorationsegmentamplitude}
      \pgfmathsetlength\pgf@ya{\pgf@randomsaw@y-4*rnd*\pgfdecorationsegmentamplitude}
      \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
      \global\pgf@randomsaw@y\pgf@ya
    \fi
  }
  \state{final}
  {}
}
\begin{tikzpicture}
    \draw[demo decoration={random saw,segment length=4pt,amplitude=2pt,meta-amplitude=20pt}](A)to[bend left](B);
\end{tikzpicture}



\end{document}

相關內容