# Add a legend to drawn functions in Tikz

I have 6 functions in a Tikzpicture, and I want to create a legend in the top left corner, so that all colours are identified with a value for p.

So I would like to create a legend in a box that indicates: black p=0, green p=0.025, red p=0.05, blue p=0.1, magenta p=0.25, brown p=0.5 (this would include a small line with the respective colour along with the p value).

I tried \addlegendentry{hello}, but this did not work for me. My code is as follows. Warning, the scale is messy because I scaled it precisely to be a certain dimension.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots,fp}
\usetikzlibrary{fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}[scale=0.225277]
\clip (-3.95,-2) rectangle (30,30);
{
\draw
plot[domain=0:30, range=0:30, variable=\x,samples=90]
({\x},{max(2*\x^2/(6.7+\x),0)});
\draw[green]
plot[domain=0:30, range=0:30, variable=\x,samples=90]
({\x},{max(2*\x^2.06/(\x^(2.06-0.9)+6.7+(\x^2.5)/1000),0)});
\draw[red]
plot[domain=0.9:30, range=0:30, variable=\x,samples=90]
({\x},{max(2*(abs(\x-0.9))^1.844/((abs(\x-0.9))^(1.844-0.9)+3.7+((abs(\x-0.9))^2.45)/500),0)});
\draw[blue]
plot[domain=1.7:30, range=0:30, variable=\x,samples=90]
({\x},{max(2*(abs(\x-1.7))^1.6/((abs(\x-1.7))^(0.7)+2.6+((abs(\x-1.7))^2.37)/500),0)});
\draw[magenta]
plot[domain=3.3:30, range=0:30, variable=\x,samples=90]
({\x},{max(2*(abs(\x-3.3))^1.07/((abs(\x-3.3))^(1.07-0.9)+1+((abs(\x-3.3))^2.1)/800),0)});
\draw[brown]
plot[domain=5.9:30, range=0:30, variable=\x,samples=90]
({\x},{max(2.15*(abs(\x-5.9))^0.88/((abs(\x-5.9))^(0.09)+1+((abs(\x-5.9))^2)/1000),0)});
}
\draw (0,0) rectangle (30,30);
\node at (28,-.9) {$v_1$};
\node at (-2.1,28) {$MRS$};
\end{tikzpicture}
\end{document}

• You are loading pgfplots, but not using it. But I like your border, +1. ;-) – user121799 Oct 5 '18 at 22:23

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:30,ymin=0,ymax=30,samples=90,legend pos=north west,no
marks,legend entries={1,...,6}]