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}
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".



