[Tex/LaTex] Timeline in LaTeX

tikz-pgftikz-timingtimeline

I am trying to create a timeline similar to the one displayed below, it does not have to be exactly the same. I already tried looking at other threats about timelines, which made me to believe I am probably gonna have to use the Tikz package. But other than that, they didn't really help me much further.

enter image description here

This is what I tried:

\documentclass{article}
\usepackage{tikz}
\begin{document}         
\begin{tikzpicture}
% draw horizontal line   
\draw[ultra thick, ->] (0,0) -- (\ImageWidth,0);

% draw vertical lines
\foreach \x in {2,4,6,8,10,12}
\draw (\x cm,3pt) -- (\x cm,-3pt);

% draw node
\draw[ultra thick] (4,0) node[below=3pt,thick] {t-2} node[above=3pt] {};
\draw[ultra thick] (6,0) node[below=3pt,thick] {t-1} node[above=3pt] {};
\draw[ultra thick] (8,0) node[below=3pt, thick] {t} node[above=3pt] {};
             \draw[ultra thick] (10,0) node[below=3pt] {t+1} node[above=3pt] {};

\draw [black, ultra thick ,decorate,decoration={brace,amplitude=5pt},
       xshift=5pt,yshift=-4pt] (4,0.5)  -- (8,0.5) 
       node [black,midway,above=4pt,xshift=-2pt] {\footnotesize Training period};


\draw [ black, ultra thick,decorate,decoration={brace,amplitude=5pt},
       xshift=8pt,yshift=-11pt] (10,-0.5) -- (8,-0.5)
       node [black,midway,below=4pt,xshift=8pt] {\footnotesize Testing period};
\end{tikzpicture}

\end{document}

It ended up looking as follows: enter image description here

Which is rather far from my desired result.

Best Answer

You can also use line width to draw the colored rectangle. And define the color shading within the \foreach.

\documentclass{article}
\newcommand{\ImageWidth}{11cm}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,positioning, arrows.meta}

\begin{document}         
\begin{tikzpicture}
% draw horizontal line   
\draw[thick, -Triangle] (0,0) -- (\ImageWidth,0) node[font=\scriptsize,below left=3pt and -8pt]{years};

% draw vertical lines
\foreach \x in {0,1,...,10}
\draw (\x cm,3pt) -- (\x cm,-3pt);

\foreach \x/\descr in {4/t-2,5/t-1,6/t,7/t+1}
\node[font=\scriptsize, text height=1.75ex,
text depth=.5ex] at (\x,-.3) {$\descr$};

% colored bar up
\foreach \x/\perccol in
{1/100,2/75,3/25,4/0}
\draw[lightgray!\perccol!red, line width=4pt] 
(\x,.5) -- +(1,0);
\draw[-Triangle, dashed, red] (5,.5) --  +(1,0);

% colored bar down
\foreach \x/\perccol in
{3/100,4/75,5/0}
\draw[lightgray!\perccol!green, line width=4pt] 
(\x,-.7) -- +(1,0);
\draw[-Triangle, dashed, green] (6,-.7) --  +(1,0);

% braces
\draw [thick ,decorate,decoration={brace,amplitude=5pt}] (4,0.7)  -- +(2,0) 
       node [black,midway,above=4pt, font=\scriptsize] {Training period};
\draw [thick,decorate,decoration={brace,amplitude=5pt}] (6,-.9) -- +(-1,0)
       node [black,midway,font=\scriptsize, below=4pt] {Testing period};

\end{tikzpicture}

\end{document}

enter image description here

Related Question