\begin{tikzpicture} \pgfdeclarelayer{background} \pgfdeclarelayer{foreground} \pgfsetlayers{background,main,foreground} %--------Left axis-------- \begin{axis}[width=6.0cm,height=5.0cm,scale only axis, unbounded coords=jump, stack plots=y,area style, xticklabel style={font=\scriptsize}, xlabel style={font=\scriptsize}, yticklabel style={font=\scriptsize}, ylabel style={font=\scriptsize}, xmin=10.0,xmax=60.0, ymin=20.0,ymax=40.0, xlabel={target time}, ylabel={agents}, legend entries={ready, talk}, legend style={font=\scriptsize,rounded corners=2pt, at={(rel axis cs:0.02,0.50)},anchor=west}] \addlegendimage{area legend,black,no markers,fill=green} \addlegendimage{area legend,black,no markers,fill=blue} \addplot [mark=none,fill=green] coordinates { (10,30)(12.7,30)(12.7,30)(28.45,30)(28.45,30)(53.83,30)(53.83,30) (60,30)}\closedcycle; \addplot [mark=none,fill=blue] coordinates { (10,6)(12.7,6)(12.7,5)(28.45,5)(28.45,4)(53.83,4)(53.83,3) (60,3)}\closedcycle; %\begin{pgfonlayer}{foreground} \draw[dotted](axis cs:12.70,20.00)--(axis cs:12.70,40.00); \draw[dotted](axis cs:28.45,20.00)--(axis cs:28.45,40.00); \draw[dotted](axis cs:53.83,20.00)--(axis cs:53.83,40.00); %\end{pgfonlayer} \draw[dotted](axis cs:60.00,20.00)--(axis cs:60.00,40.00); \end{axis} \end{tikzpicture}