Add the legend to the function.