
Wie kann man eine Figur wie diese am einfachsten erzeugen? Die Kurve soll wackelig und zufällig aussehen und immer nicht ansteigen, außer bei den Sprüngen nach oben. Ich habe versucht, Dekorationen mit zufälligen Schritten zu verwenden, aber das sieht nicht richtig aus und nimmt manchmal zu. Ich habe auch ein Diagramm mit vielen einzelnen Koordinaten ausprobiert, die ich zufällig generiert habe, aber das sah nicht glatt genug aus.
Ich verwende gerne entweder einfaches TikZ oder pgfplots.
Bearbeiten:Hier sind zwei Versuche.
Der erste verwendet random steps
Dekoration, aber die Handlung ist nicht glatt und steigert sich manchmal. Ich habe ein paar verschiedene Amplituden ausprobiert.
\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}
Der zweite Versuch verwendet pgfplots mit vielen fein verteilten Koordinaten (die ich zufällig in Excel generiert habe). Dieses hier kommt näher heran, ist aber zu feinkörnig und nicht glatt genug.
\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}
Antwort1
Schauen wir uns zunächst die Dekoration an 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}
Ich kannglättenes von \pgfsetcornersarced
. Also habe ich eine Dekoration namens erstellt 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}
Und dann füge ich einigehochspringen. Hier verwende ich \pgf@randomsaw@y
zum Speichern der y-Koordinate und füge eine negative Zufallslänge hinzu, sodass die Funktion nicht zunimmt.
\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}