I am using a plain-old tikzpicture to produce a pie-chart-like plot. Now, all other plots in my paper are handled using pgfplots (which doesn't do pie charts) and have their legend typeset by pgfplots.

It would thus be great if it were possible to create, re-using as many keys like /pgfplots/area legend as possible, to create a pgfplots-style legend in TiKZ. Has anyone done anything like this before?

  • 2
    Would you mind uploading an example of how you create your pie charts?
    – Jake
    Commented May 7, 2012 at 15:28
  • I don't use any special package for that; just plain-old tikzpicture \filldraw and arc paths. (No package could easily do what I wanted to out of the box: a pie chart within a ring chart; sort of like a pie with a crust.) Commented May 8, 2012 at 7:20

2 Answers 2


Pgfplots has a couple of helper methods which do the job - and these methods are mostly independent of any axis. The only required things are the text labels and the styles for the individual images.

Here is a short draft which might do what you want:



% argument #1: any options
    % inits/clears the lists (which might be populated from previous
    % axes):
    \csname pgfplots@init@cleared@structures\endcsname
    % draws the legend:
    \csname pgfplots@createlegend\endcsname

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



    \begin{customlegend}[legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}


The different \addlegendimage styles are a little bit ... inconsistent, but I wanted to experiment if they work. I guess that mesh legend might produce problems, so stick to the simpler ones...

enter image description here

  • Works like a charm. Commented May 8, 2012 at 8:17
  • 1
    This is really nice. I just don't know how I position the legend in the tikzpicture at the right spot. Tried to put it into a node, but got an error. Is there a general way to position something?
    – Jana
    Commented Jul 4, 2012 at 13:52
  • @Jana it is placed using the legend keys, i.e. add legend style={at={(0.5,0.5)},anchor=center} to the option list. Perhaps you can also omit the outer legend style and write at=... immediately. Commented Jul 7, 2012 at 20:44
  • 1
    If I'm not mistaken, this positioning using legend style={at={(0.5,0.5)},anchor=center} is to position the legend as if it was inside an axis environment. How should be done in order to position the legend using global coordinates of the tikz? Lets say that the legend should be a node positioned at (100,100)
    – Sik
    Commented Jun 16, 2014 at 6:28
  • 1
    at={(100,100)} should work. Commented Jun 16, 2014 at 18:07

And here is how to put a non-pgfplots style legend into pgfplots.



\node[draw=black,thick,rounded corners=2pt,below left=2mm] at (border.north east) {%
\begin{tabular}{@{}r@{ }l@{}}
 \raisebox{2pt}{\tikz{\draw[black] (0,0) -- (5mm,0);}}&black\\
 \raisebox{2pt}{\tikz{\draw[red] (0,0) -- (5mm,0);}}&red\\
 \raisebox{2pt}{\tikz{\draw[green] (0,0) -- (5mm,0);}}&green\\
 \raisebox{2pt}{\tikz{\draw[blue] (0,0) -- (5mm,0);}}&blue


plot with legend

  • Instead of a \fbox{} I would use the tikz-node which contains the legend to draw the border. This way, you can easily specify line widths, colors and shapes. Just omit the \fbox{PRESERVE THE CONTENT} and add draw, rounded corners or the like afer the \node[below left=1mm. Commented Feb 9, 2015 at 13:10
  • @PaulPaulsen - You obviously already know how to do it, but okay. Commented Feb 9, 2015 at 17:12
  • 2
    Yeah, but I myself learned a lot of things like this from comments onother questions, so I thought I would just post it here in case somebody is looking for that... Commented Feb 9, 2015 at 21:15

You must log in to answer this question.

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