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.

I'm having problems finding how to use forget plot in tikz. I need only 2 legendentries like this: enter image description here

but get a massive long list of every object in the graph. This post addresses the problem but seems to be different to the way I have my code.

NEW CODE:

     \documentclass{amsart}
          \usepackage{tikz,pgfplots}
         \pgfplotsset{compat=newest} 
         \pgfplotsset{plot coordinates/math parser=false}
        \usetikzlibrary{plotmarks}

\newenvironment{customlegend}[1][]{%
        \begingroup
        % inits/clears the lists (which might be populated from previous
        % axes):
        \csname pgfplots@init@cleared@structures\endcsname
        \pgfplotsset{#1}%
    }{%
        % draws the legend:
        \csname pgfplots@createlegend\endcsname
        \endgroup
    }%

    % makes \addlegendimage available (typically only available within an
    % axis environment):
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

    %%--------------------------------

    % definition to insert numbers
    \pgfkeys{/pgfplots/number in legend/.style={%
            /pgfplots/legend image code/.code={%
                \node at (0.125,-0.0225){#1}; % <= changed x value
            },%
        },
    }
    \pgfplotsset{
    every legend to name picture/.style={west}
    }


        \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=-2, ymax=5,
        ylabel={Normalised cfu (Y) values},
        legend style={draw=black,fill=white,legend cell align=left}
        ]
        \addplot [
        color=black,
        dotted
        ]
        table{
        0.825 5
        0.825 2
        };
        ;

        \addplot [
        color=black,
        solid
        ]
        table{
        0.755 5
        0.895 5
        };
        ;

        \addplot [
        color=black,
        solid
        ]
        table{
        0.755 2
        0.895 2
        };
        ;

        \draw[solid, fill=mycolor1, draw=black] (axis cs:0.685, 3) rectangle (axis cs:0.965, 4);
        ;

        \addplot [
        color=black,
        solid,
        line width=1.0pt
        ]
        table{
        0.685 3
        0.965 3
        };
        ;

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

        \addplot [
        color=black,
        dotted
        ]
        table{
        1.175 5
        1.175 -2
        };
        ;

        \addplot [
        color=black,
        solid
        ]
        table{
        1.105 5
        1.245 5
        };
        ;

        \addplot [
        color=black,
        solid
        ]
        table{
        1.105 -2
        1.245 -2
        };


        \draw[solid, fill=mycolor2, draw=black] (axis cs:1.035, 0) rectangle (axis cs:1.315, 3);
        \addlegendentry{6 ACH};

        \addplot [
        color=black,
        solid,
        line width=1.0pt
        ]
        table{
        1.035 2
        1.315 2
        };


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


        \end{axis}
        \end{tikzpicture}%
    \begin{customlegend}[legend cell align=left, %<= to align cells
    legend entries={ % <= in the following there are the entries
     4 ACH,
     6 ACH
    },
    legend style={at={(15,8)},font=\footnotesize}] % <= to define position and font legend
    % the following are the "images" and numbers in the legend
        \addlegendimage{area legend,black,fill=mycolor1}
        \addlegendimage{area legend,black,fill=mycolor2}
    \end{customlegend}
        \end{document}
share|improve this question
    
Can you explain better what you want? With your code I only get one entry in the legend. –  cacamailg Mar 16 '13 at 22:32
    
I'd like to make the legend look like the picture above. Either artificially by drawing the two blue boxes as nodes or by adding a legendentry. The problem is that I can't find the plot objects that make them. Any thoughts how to do this? –  HCAI Mar 17 '13 at 10:27
add comment

1 Answer

up vote 3 down vote accepted

Your example appears to consist of a lot of custom drawing instructions which make up the box plot -- and the actual box (which seems to be relevant for the legend) is a \draw statement.

In this case, it appears to be appropriate to use \addlegendimage combined with \addlegendentry in order to actually draw the legend - and to provide forget plot to every \addplot command:

\documentclass{amsart}
  \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=-2, ymax=5,
ylabel={Normalised cfu (Y) values},
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [
forget plot,
color=black,
dotted
]
table{
0.825 5
0.825 2
};
;

\addplot [
forget plot,
color=black,
solid
]
table{
0.755 5
0.895 5
};
;

\addplot [
forget plot,
color=black,
solid
]
table{
0.755 2
0.895 2
};
;

\draw[solid, fill=mycolor1, draw=black] (axis cs:0.685, 3) rectangle (axis cs:0.965, 4);
\addlegendimage{area legend,fill=mycolor1,draw=black}
\addlegendentry{4 ACH};
;

\addplot [
forget plot,
color=black,
solid,
line width=1.0pt
]
table{
0.685 3
0.965 3
};
;

\addplot [
forget plot,
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor1,draw=black}
]
table{
0.825 3.56
};
;

\addplot [
forget plot,
color=black,
dotted
]
table{
1.175 5
1.175 -2
};
;

\addplot [
forget plot,
color=black,
solid
]
table{
1.105 5
1.245 5
};
;

\addplot [
forget plot,
color=black,
solid
]
table{
1.105 -2
1.245 -2
};


\draw[solid, fill=mycolor2, draw=black] (axis cs:1.035, 0) rectangle (axis cs:1.315, 3);
\addlegendimage{area legend,fill=mycolor2,draw=black}
\addlegendentry{6 ACH};

\addplot [
forget plot,
color=black,
solid,
line width=1.0pt
]
table{
1.035 2
1.315 2
};


\addplot [
forget plot,
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor2,draw=black}
]
table{
1.175 1.59
};


\end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

share|improve this answer
    
Fantastic! Thank you very much! This looks like the best solution. See edit for an alternative method I found, albeit there I need to find individual coordinates for positioning of the legend box. –  HCAI Mar 17 '13 at 20:21
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.