Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The legend from a boxplot figure is not being created properly.

I get this: enter image description here

Instead of thiS: enter image description here But really only need 2 legend entries. Any Ideas why all the objects in this plot are being given legend entries when in Matlab there are only 2 light blue=4ACH and dark blue=6ACH.

How do I make the two blue boxes using tikz code? \addlegendentry doesn't work because the boxplots are not plot items but filled objects defined by tables of coordinates

MVE

\documentclass{amsart}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{subfig}
%\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode} %matlab to latex s
\usepackage{tikz,pgfplots}
 \pgfplotsset{compat=newest} 
 \pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{plotmarks}
\begin{document}

\definecolor{mycolor1}{rgb}{0.6,0.8,1}
\definecolor{mycolor2}{rgb}{0,0.2,0.6}

\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
scale only axis,
xmin=0.5, xmax=1.5,
xtick={1},
xticklabels={Direct Care,Housekeeping,Mealtimes,Medication Round,Miscellaneous,Personal Care},
ymin=-10, ymax=100,
ytick={0,10,20,30,40,50,60,70,80,90,100},
ylabel={Normalised cfu (Y) values},
legend style={draw=black,fill=white,legend cell align=left}
]

\addplot [
color=black,
solid
]
table{
0.755 0
0.895 0
};
\addlegendentry{4 ACH};

\addplot [
color=black,
solid
]
table{
0.755 0
0.895 0
};
\addlegendentry{4 ACH};

\addplot [
color=black,
solid,
line width=1.0pt
]
table{
0.685 0
0.965 0
};
\addlegendentry{4 ACH};

\addplot [
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor1,draw=black}
]
table{
0.825 10.0567798116818
};
\addlegendentry{200 ACH};
\addplot [
color=black,
dotted
]
table{
0.825 0
0.825 0
};
\addlegendentry{4 ACH};
\addplot [
color=black,
solid
]
table{
0.755 0
0.895 0
};
\addlegendentry{4 ACH};

\addplot [
color=black,
solid
]
table{
0.755 0
0.895 0
};
\addlegendentry{4 ACH};

\addplot [
color=black,
solid,
line width=1.0pt
]
table{
0.685 0
0.965 0
};
\addlegendentry{4 ACH};

\addplot [
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor1,draw=black}
]
table{
0.825 10.0567798116818
};
\addlegendentry{4 ACH};

\addlegendentry{6 ACH};

\addplot [
color=black,
dotted
]
table{
1.175 0
1.175 0
};
\addlegendentry{6 ACH};

\addplot [
color=black,
solid
]
table{
1.105 0
1.245 0
};
\addlegendentry{6 ACH};

\addplot [
color=black,
solid
]
table{
1.105 0
1.245 0
};
\addlegendentry{500 ACH};

\addplot [
color=black,
solid,
line width=1.0pt
]
table{
1.035 0
1.315 0
};
\addlegendentry{6 ACH};

\addplot [
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor2,draw=black}
]
table{
1.175 10.6896518156056
};
\addlegendentry{6 ACH};


\end{axis}
\end{tikzpicture}%
\end{document}
share|improve this question
    
Every \addlegendentry will produce a legend entry. This is more Matlab2TikZ’ wrong-doing than pgfplots’. Would manually removing the additional \addlegendentrys be a solution? –  Qrrbrbirlbel Mar 16 '13 at 4:00
    
This is what I thiught too, but I cant seem to find the correct legend entry fornthe blue boxes. Can you replicate those using tikz commands of some sort? –  HCAI Mar 16 '13 at 8:03
2  
You can use area legend for that. See the manual. –  percusse Mar 17 '13 at 12:22
    
@percusse tex.stackexchange.com/questions/62262/legend-in-tikzpicture Shows a good example of your suggestion and it partially works. Is it possible to automatically position the legend depending on the size of the graph? I have 100 graphs of different sizes and it would be very teadious to work out the coordinates of the upper right corner for each one. Any thoughts? If you make this an answer I'll give you thumbs up. Cheers –  HCAI Mar 17 '13 at 14:04
    
@percusse Could you expand your comment into an answer? –  Joseph Wright Aug 10 '13 at 10:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.