1

I'm making a plot using rectangles that I drew manually, instead of using the standard plot types defined in PGFPlots. Here is an MWE:

\documentclass{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture} \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=4] \draw[fill=blue!50] (0,0) rectangle (1,1); \draw[fill=green!50] (1,0) rectangle (4,3); \draw[fill=red!50] (4,0) rectangle (5,2); \end{axis} \end{tikzpicture}

\end{document}

enter image description here

Question: How do I add a legend to the plot? I want to label each of the three rectangles, e.g., rectangle 1 "blue", rectangle 2 "green", and rectangle 3 "red".

  • You could add a node (or three nodes) in the top right corner of your drawing, with the information you need in the node's curly brackets. – Daniel N Jun 22 '21 at 04:53
  • @DanielN I'm not sure what you mean, could you please provide an example? – I Like to Code Jun 22 '21 at 08:11
  • See https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855?r=SearchResults&s=1|56.7385#148855 – John Kormylo Jun 22 '21 at 12:29

3 Answers3

3

You can use \addlegendimage:

enter image description here

\documentclass{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture} \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=4] \draw[fill=blue!50] (0,0) rectangle (1,1); \draw[fill=green!50] (1,0) rectangle (4,3); \draw[fill=red!50] (4,0) rectangle (5,2);

\addlegendimage{area legend, fill=blue!50} \addlegendimage{area legend, fill=green!50} \addlegendimage{area legend, fill=red!50}

\legend{foo, bar, baz} \end{axis} \end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688
1

enter image description here

Something like this?

  • I used tikz since you don't really need pgfplots, if I understand correctly your question.
  • In the node that gives the legend, I used two different elements...

The code

\documentclass[11pt, border=1cm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture} \draw[blue, fill=blue!50] (0, 0) rectangle (1, 1); \draw[green, fill=green!50] (1, 0) rectangle (4, 3); \draw[red, fill=red!50] (4, 0) rectangle (5, 2);

% the legend \path (5, 5) node[draw, scale=.6, anchor=north west, text width=4.4em] {% $1$ = blue $2$ = green $3$ = \tikz{\draw[red, fill=red!50] (0, 0) rectangle (1em, 1.5ex);} };

% the axes \draw[->] (0, 0) -- (6, 0) node[below, scale=.8] {$x$}; \draw[->] (0, 0) -- (0, 5) node[left, scale=.8] {$y$}; \foreach \i in {1, ..., 5}{ \draw (\i, 2pt) -- ++(0, -4pt) node[below, scale=.7] {$\i$}; } \foreach \j in {1, ..., 4}{ \draw (2pt, \j) -- ++(-4pt, 0) node[left, scale=.7] {$\j$}; } \end{tikzpicture} \end{document}

Daniel N
  • 5,687
1

This uses a tabular inside a savebox to construct the legend.

Note that one really should but \sbox0 inside an environment or group to preserve its global contents.

\documentclass{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}

\begin{document}

\sbox0{\begin{tabular}{rl} \tikz{\node [fill=blue!50, inner sep=1ex]{};} & blue\ \tikz{\node [fill=green!50, inner sep=1ex]{};} & green\ \tikz{\node [fill=red!50, inner sep=1ex]{};} & red \end{tabular}}

\begin{tikzpicture} \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=4,name=border] \draw[fill=blue!50] (0,0) rectangle (1,1); \draw[fill=green!50] (1,0) rectangle (4,3); \draw[fill=red!50] (4,0) rectangle (5,2); \end{axis} \node[below=1cm, draw, rounded corners] at (border.south) {\usebox0}; \end{tikzpicture}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120