# Desperately Looking for a tikZ MWE

I'm looking for a basic MWE on how to create a tikZ graph (or any other environment) along the lines of the attached picture. I'm able to create one simple coords system but that's about it... What's really bugging me is that it doesn't look very complicated but I ran out of ideas...

Thanks folks!

• Welcome to Te.SX. The tikz manual is full of examples. – Johannes_B Nov 17 '19 at 17:21
• This can be build using rectangle and normal lines. The hardest thing are the hatched rectangles, but you should be able to find a fill pattern that looks like this with a quick search on the internet. So you'll need \draw, rectangle, \draw[fill=gray], \node and pattern, and for the dashed arrows \draw[->,dashed]. – Skillmon likes topanswers.xyz Nov 17 '19 at 17:56
• Are you aware of the pgfgantt package? Your screen shot resembles very much a Gantt chart, so maybe this is what you need. – user194703 Nov 17 '19 at 18:19
• @fjungplan It is straightforward but tedious so people here are happy to help you when you report a concrete point at which you are stuck but they are, understandably, less eager to punch in all the texts from your screen shot. – user194703 Nov 17 '19 at 18:37
• Related: ctan.org/topic/gantt – Fran Nov 18 '19 at 21:56

If you have some repetitive tasks, it is usually a good idea to define styles and pics. This answer has a pic for the interval bar, the appearance of which is fully controlled by pgf keys. For instance,

\path[thick]
(0,0) pic[intervals/.cd,step=2cm]{intervals={1,2,3,4,\dots,12,1}};


leads to the upper interval bar. These pics make the code look long if you only use them once or twice, but they do pay off if you use them more often than that. I did not know to which extent the whole diagram is repetitive. If so, you may define a pic for it, too. Anyway, I leave this as an exercise.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns}
\tikzset{pics/intervals/.style={code={%
\def\pv##1{\pgfkeysvalueof{/tikz/intervals/##1}}%
\foreach \XX [count=\YY] in {#1}
{\draw (\YY*\pv{step},\pv{tick length}/2) -- (\YY*\pv{step},-\pv{tick length}/2);
\path (\YY*\pv{step}-0.5*\pv{step},-\pv{tick length}/2)
node[below]{\XX};
\xdef\tmpintcnt{\YY}}
\draw (0,\pv{tick length}/2) -- (0,-\pv{tick length}/2);
\draw[intervals/arrow] (0,0) -- (\tmpintcnt*\pv{step}+\pv{overshoot},0);
}},
intervals/.cd,tick length/.initial=3mm,step/.initial=1cm,tick/.style={},
arrow/.style={-stealth},overshoot/.initial=5mm,
/tikz/.cd,math node/.style={execute at begin node=$,execute at end node=$}}
\begin{document}
\begin{tikzpicture}[>=stealth]
\begin{scope}[local bounding box=upper]
\path[thick]
(0,0) pic[intervals/.cd,step=2cm]{intervals={1,2,3,4,\dots,12,1}}
(0,-2) pic[intervals/.cd,step=5mm]{intervals={1,...,15}};
% upper rectangles
\draw[fill=gray!40] (0,0) rectangle (4,1) node[midway]{frozen periods};
\draw[fill=white] (4,0) rectangle (12,1) node[midway]{open periods};
% lower rectangles
\draw[fill=gray!40] (0,-2) rectangle ++(1,1);
\draw (1,-2) rectangle ++(5,1);
% dashed arrows
\foreach \X[count=\Y] in {->,->,->}
{\draw[\X,dashed,thick] (2*\Y,0) -- (2*\Y,-1);}
% annotations on the left
\draw[thick] (0,1) -- (0,-2.5) node[pos=1/7,left] {APP}
node[pos=5/7,left] {MPS};
\end{scope}
%
\begin{scope}[local bounding box=lower,yshift=-5cm]
\path[thick]
(0,0) pic[intervals/.cd,step=2cm]{intervals={1,2,3,4,\dots,12,1}}
(0,-2) pic[intervals/.cd,step=5mm]{intervals={1,...,15}};
% upper rectangles
\draw[pattern=north west lines] (0,0) rectangle (2,1);
\draw[fill=gray!40] (2,0) rectangle (4,1);
\draw[fill=white] (4,0) rectangle (14,1);
% lower rectangles
\draw[pattern=north west lines] (0,-2)  rectangle ++(0.5,1);
\draw[fill=gray!40] (0.5,-2) rectangle ++(1,1);
\draw (1.5,-2) rectangle ++(5,1);
% dashed arrows
\foreach \X[count=\Y] in {<-,->,->} % one arrow reversed
{\draw[\X,dashed,thick] (2*\Y,0) -- (2*\Y,-1);}
% annotations on the left
\draw[thick] (0,1) -- (0,-2.5) node[pos=1/7,left] {APP}
node[pos=5/7,left] {MPS};
\end{scope}
\end{tikzpicture}
\end{document}


Apart from the above-mentioned second pic you may use e.g. the patterns.meta library to customize the pattern, and so on.

I propose a solution with pstricks – it uses LaTeX-like commands, which are an interface with the postscript language

\documentclass[border=6pt, svgnames]{standalone}

\usepackage{pst-plot, auto-pst-pdf}

\begin{document}

\psset{xunit =1.8cm,arrowinset=0.12, dimen=inner}%
\noindent \begin{pspicture}(-0.7,-2.5)(7.5,1)
%% first axis
\uput{1em}[l](0,0.5){APP}
\foreach \i/\j in {0.5/1,1.5/2,2.5/3,3.5/4,4.5/{...},5.5/12,6.5/1} {\uput{2ex}[d](\i, 0){\small\j}}
\psframe[linestyle=none, fillstyle=vlines](0,0)(1,1)
\psframe[linestyle=none, fillstyle=solid, fillcolor=Gainsboro](1,0)(2,1)
\psline(0,0)(0,1)(7,1)(7,0)\psline(1,0)(1,1)\psline(2,0)(2,1)
\psaxes[arrows=->, yAxis=false, labels=none](0,0)(0,0)(7.3,0)
% second axis
\psframe[linestyle=none, fillstyle=vlines](0,-2)(0.25,-1)
\psframe[linestyle=none, fillstyle=solid, fillcolor=Gainsboro](0.25,-2)(1,-1)
\uput{1em}[l](0,-1.5){MPS}
\psline(0, -2)(0,-1)(3.25,-1)(3.25,-2)\psline(0.25,-2)(0.25,-1)\psline(1,-2)(1,-1)
\psaxes[arrows=->, yAxis=false, labels=none, dx=0.25](0,-2)(0,-2)(4,-2)
\multido{\n = 0.125 + 0.250,\i=1+1}{15}{\uput{2ex}[d](\n, -2){\small\i}}
\psset{linestyle=dashed, arrows=->}
\psline(1,-1)(1,0)\psline(2,0)(2,-1)\psline(3,0)(3,-1)
\end{pspicture}

\end{document}


After some fiddling around with pgfgantt I was able to recreate it there too... The look can certainly be improved, but it's not too bad. I thought I'd share it here:

\begin{figure}[h!]
\caption{Rolling Horizon Planning}
\label{pic4}
\centering
\ganttsetstartanchor{south}
\ganttsetendanchor{on top=.2}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=1}
\ganttsetendanchor{on top=.6}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=.25}
\ganttsetendanchor{on top=1}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=0}
\ganttsetendanchor{on top=.5}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=.2}
\ganttsetendanchor{on top=.9}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{south}
\ganttsetendanchor{on top=.4}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=1}
\ganttsetendanchor{on top=.8}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=0}
\ganttsetendanchor{on top=0.1}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\ganttsetstartanchor{on bottom=0}
\ganttsetendanchor{on top=0}
(\xLeft, \yUpper) --
(\xRight, \yLower);
}
\gantttitle{Iteration 1}{28}\\
\gantttitlelist{1,...,4}{4}
\gantttitle{...}{4}
\gantttitlelist{12}{4}
\gantttitlelist{1}{4} \\
\gantttitlelist{1,...,16}{1} \\
\ganttbar[bar/.append style={fill=lightgray, dashed}]{APP}{1}{8}
\ganttbar{}{9}{24}\\
\ganttbar[bar/.append style={fill=lightgray, dashed}]{MPS}{1}{2}
\ganttbar{}{3}{12}\\
\gantttitle{Iteration 2}{28}\\
\gantttitlelist{1,...,4}{4}
\gantttitle{...}{4}
\gantttitlelist{12}{4}
\gantttitlelist{1}{4} \\
\gantttitlelist{1,...,16}{1} \\
\ganttbar[bar/.append style={fill=gray}]{APP}{1}{4}
\ganttbar[bar/.append style={fill=lightgray, dashed}]{}{5}{8}
\ganttbar{}{9}{28}\\
\ganttbar[bar/.append style={fill=gray}]{MPS}{1}{1}
\ganttbar[bar/.append style={fill=lightgray, dashed}]{}{2}{3}
\ganttbar{}{4}{13}\\
\gantttitle{Iteration 3}{28}\\
\gantttitlelist{1,...,4}{4}
\gantttitle{...}{4}
\gantttitlelist{12}{4}
\gantttitlelist{1}{4} \\
\gantttitlelist{1,...,16}{1} \\
\ganttbar[bar/.append style={fill=gray}]{APP}{1}{4}
\ganttbar[bar/.append style={fill=lightgray, dashed}]{}{5}{12}
\ganttbar{}{13}{28}\\
\ganttbar[bar/.append style={fill=gray}]{MPS}{1}{2}
\ganttbar[bar/.append style={fill=lightgray, dashed}]{}{3}{4}
\ganttbar{}{5}{14}