I'm trying to build a simple 1-dimension random walk. I use \foreach and want to do an increment of a x (to a step) and y (to an epsilon).
\pgfmathsetseed{1}
\begin{tikzpicture}
\begin{axis}[
xmin=2018, xmax=2040,
ymin=0, ymax=200,
xlabel={$year$}, ylabel={$value$},
axis x line=bottom,
axis y line=center]
\coordinate (start) at (axis cs:2018,100);
\foreach \year in {2018,...,2040} {
\coordinate (next) at ($ (start) + (1, rand*5) $);
\draw[red] (start) -- (next);
\coordinate (start) at (next);
}
\end{axis}
\end{tikzpicture}
I have an error "Dimension too large". If I set a dimension (e.g. cm) to the line
\coordinate (next) at ($ (start) + (0.5cm, rand*0.5cm) $);
Then the code works fine but the step is not relevant to axis anymore. Is there a way to make xshift equal one step on the axis withouth hardcoding axis width in cm and specifying the corresponding xshift in cm as well?
year
norvalue
are mathematical expressions, so don't write them in math mode. Usexlabel={year}, ylabel={value}
. (Unless of course you use an actual variable, and not the words.)