7

I am curious if it is possible to use a custom image (e.g. plot) instead of text inside the legend of a chart generated with pgfplots. Specifically, given this MWC:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[xlabel=$x$, ymin=0, xmin=-1.1, xmax=1.1, samples=300, ylabel=$f(x)$,
                 smooth,
                 legend style={ legend cell align=left, at={(1.03, 1)}, anchor=north west}
                 ]
        \addplot coordinates {
                              (-1, 0)
                              (0, 0.5)
                              (1, 1)
                              };
        \addlegendentry{Uniform(0,1)};
        \addplot coordinates {
                              (-1, 1)
                              (0, 0.2)
                              (1, 0)
                              };
        \addlegendentry{Exponential(1)};
    \end{axis}
    \end{tikzpicture}
\end{document}

which produces the chart below

http://i.imgur.com/T2zVNe5.png

I would like to insert the actual density function of a Uniform distribution in the legend text, e.g. I want to see this (scaled down)

PDF of Uniform distribution

instead of the "Uniform(0, 1)" text. Of course, similar for the Exponential(1) part.

The Uniform PDF plot is also generated with pgfplots (MWC below) so the question, perhaps, is if it possible to embed a pgfplot inside a legend entry.

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[ymin=0, xmin=0, xmax=1, ymax=2,xticklabels=none, yticklabels=none]
        \addplot[color=black, fill=black] (x,1) \closedcycle;
    \end{axis}
    \end{tikzpicture}
\end{document}

Later Edit

Torbjorn's proposed solution works; it does what I want:

Plot with two images in legend

I will let the question open to see if there is another possibility to generate everything on the fly, without embedding previously compiled files.

2 Answers 2

10

(This is a completely rewritten revision of my initial answer)

In order to modify the description of a legend entry, one can use \addlegendentry, legend entries, or \legend. Consequently, @Torbjorns answer is the way to go.

However, you are free to insert small pictures into the legend text; there is no restriction to text. To this end, you can simply write \tikz ... ; instead of, say, "Uniform".

Here is an approach based on @Torbjorns answer and my attempts to assemble a more-or-less suitable image:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[xlabel=$x$, ymin=0, xmin=-1.1, xmax=1.1, samples=300, ylabel=$f(x)$,
                 smooth,
                 legend style={ legend cell align=left, at={(1.03, 1)}, anchor=north west}
                 ]
        \addplot coordinates {
                              (-1, 0)
                              (0, 0.5)
                              (1, 1)
                              };
        \addlegendentry{
                \begin{tikzpicture}
                    \fill[draw] (0cm,0cm) rectangle (0.6cm,-0.15cm);
                    \draw (0cm,0cm) rectangle (0.6cm,+0.15cm);
                \end{tikzpicture}
        }
        \addplot  coordinates {
                              (-1, 1)
                              (0, 0.2)
                              (1, 0)
                              };
        \addlegendentry{
                \tikz\draw[mark=none,samples=11,domain=-2.5:2.5,xshift=1em,yshift=-0.05cm,xscale=0.015,yscale=0.03]  plot (\x,{exp(-(\x)^2)});
        }
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

6
  • I am little familiar with this feature, but note that your approach does not answer my question. I want to keep the legend image (i.e. the straight line with marker) and substitute a second image for the accompanying text. Your approach still has the "Uniform (0,1)" text, which I want replaced. Can I hook into a keyword that controls the label appearance?
    – baudolino
    Dec 23, 2013 at 20:59
  • Ah, I see. Apparently, I misunderstood the question. I read your edited question right now. Why do you refer to Torbjorns answer as "hack"? If you want some "other description text", the way to insert it is to follow his direction. If needed, I would expect that you can combine my answer with the one of Torbjorn (i.e. that you can generate the graphs instead of including an external picture). Dec 24, 2013 at 9:09
  • I think the more elegant solution is to have the entire code compile in a single run, without including files compiled previously. Basically, just how you can use legend image code to manipulate the legend image, I am wondering if there exists something like legend text code that gives access to the legend text.
    – baudolino
    Dec 24, 2013 at 17:09
  • I understand; my point was that you can replace Torbjorns image by a tikz picture. I have edited my answer to show what I mean. Dec 25, 2013 at 8:28
  • Perfect. This is exactly what I meant, and I think the value of the question is now that one can embed tikz code inside the addlegendentry{} keyword.
    – baudolino
    Dec 25, 2013 at 17:38
4

Not a direct answer, but a workaround at least: You can use \includegraphics in a legend entry, so you could first compile the code generating the uniform distribution and then use

\addlegendentry{\includegraphics[width=1cm]{FilenameOfPDFwithUniformDist}};

to add the image in the legend.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .