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}

相关内容