I have a problem with formatting the legend correctly. Here is my code:
\begin{figure}[H]
\centering
\begin{tikzpicture}
\pgfplotstableread{
x Order m0 m132 m3 m460 m6
0 gtx780-cb-s 0.57615 0.627627 0.394365 0.811266 1.05492
1 gtx780-mu-s 0.141668 0.315022 0.145579 0.202026 0.253015
4 gtx780-cb-d 3.784450 10.841200 5.527380 5.881210 8.342070
5 gtx780-mu-d 0.279953 0.918595 0.340272 0.471707 0.422749
8 k40-cb-s 0.695936 0.831193 0.476254 1.05996 1.26672
9 k40-mu-s 0.193261 0.434555 0.195305 0.303927 0.338011
12 k40-cb-d 1.004380 2.401120 1.314340 1.560650 2.067400
13 k40-mu-d 0.377453 1.200990 0.377581 0.598974 0.674917
}\datatable
\begin{axis}[
x post scale=\linewidth/\axisdefaultwidth,
xlabel=Time {[}ms{]},
ytick=data,
yticklabels from table={\datatable}{Order},
xbar stacked,
xmin=0,
ymax=15,
legend style={area legend,at={(0.5,-0.3)},anchor=north,legend columns=-1},
]
\addplot table [x expr={4 * \thisrow{m0}}, y=x] {\datatable};
\addplot table [x=m132, y=x] {\datatable};
\addplot table [x=m3, y=x] {\datatable};
\addplot table [x=m460, y=x] {\datatable};
\addplot table [x=m6, y=x] {\datatable};
\addlegendimage{blue,sharp plot}
\addlegendimage{red,sharp plot}
\draw[blue] (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/xmax});
\draw[red] (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/xmax});
\legend{M0,M132,M3,M460,M6,line1,line2};
\end{axis}
\end{tikzpicture}
\end{figure}
And the output I get is:
As you can see the legend is not formatted nicely. I want little color areas for the bars and two lines for the two lines I've added to the plot. Also, how do I bring the lines on top of the bars?
Thanks!
Best Answer
As cmhughes mentioned in his comment, adding the option
area style
to theaxis
environment gives the desired formatting for the legend.Draw the lines before adding the plots.
The code: