How to plot a timeline

I find PGFPlotsX.jl very useful for these situations where there exists a solution which you can copy and modify for use in Julia. The solution for this plot can be found on stackexchange.

The following example shows how PGFPlotsX can be used for copying an existing solution, this is by no means best practices and is essentially just copying of text. Note that there exists several options in the documentation of the package to facilitate the transfer of data from Julia to fit you needs when creating the plot.

using PGFPlotsX

#Add necessary preamble information once
if !in(raw"\usetikzlibrary{decorations.pathreplacing}",PGFPlotsX.CUSTOM_PREAMBLE)
    push!(PGFPlotsX.CUSTOM_PREAMBLE,
    raw"\usetikzlibrary{decorations.pathreplacing}
        \definecolor{myLightGray}{RGB}{191,191,191}
        \definecolor{myGray}{RGB}{160,160,160}
        \definecolor{myDarkGray}{RGB}{144,144,144}
        \definecolor{myDarkRed}{RGB}{167,114,115}
        \definecolor{myRed}{RGB}{255,58,70}
        \definecolor{myGreen}{RGB}{0,255,71}")
end

function plot_tline()
       
    tp = @pgf TikzPicture(
    {
        "every node/.style"={
        font=raw"\scriptsize",
        text_height="1ex",
        text_depth=".25ex",
        },
    },)

    push!(tp,
        raw"% draw horizontal line   
        \draw[->] (0,0) -- (8.5,0);
        
        % draw vertical lines
        \foreach \x in {0,1,...,8}{
            \draw (\x cm,3pt) -- (\x cm,0pt);
        }
        
        % place axis labels
        \node[anchor=north] at (3,0) {$t-2$};
        \node[anchor=north] at (4,0) {$t-1$};
        \node[anchor=north] at (5,0) {$t$};
        \node[anchor=north] at (6,0) {$t+1$};
        \node[anchor=north] at (8.5,0) {years};
        
        % draw scale above
        \fill[myLightGray] (1,0.25) rectangle (2,0.4);
        \fill[myDarkGray] (2,0.25) rectangle (3,0.4);
        \fill[myDarkRed] (3,0.25) rectangle (4,0.4);
        \fill[myRed] (4,0.25) rectangle (5,0.4);
        \draw[myRed,dashed,thick,-latex] (5.05,0.325) -- (6.05,0.325);
        
        % draw scale below
        \fill[myLightGray] (3,-0.4) rectangle (4,-0.55);
        \fill[myGray] (4,-0.4) rectangle (5,-0.55);
        \fill[myGreen] (5,-0.4) rectangle (6,-0.55);
        \draw[myGreen,dashed,thick,-latex] (6.05,-0.475) -- (7.05,-0.475);
        
        % draw curly braces and add their labels
        \draw[decorate,decoration={brace,amplitude=5pt}] (3,0.45) -- (5,0.45)
            node[anchor=south,midway,above=4pt] {Training period};
        \draw[decorate,decoration={brace,amplitude=5pt}] (6,-0.6) -- (5,-0.6)
            node[anchor=north,midway,below=4pt] {Testing period};"
    )
    return tp
end

tp=plot_tline()
display(tp)
#pgfsave("tline.tikz", tp)
1 Like