WeCodeAcademy

Legends

When a chart has more than one series, the reader needs a key. Give each series a label= when you draw it, then call ax.legend() once. The legend reads the labels automatically, so the names stay linked to the lines they describe.

Keep labels short. Where it is clear, labelling a line directly at its end is even better than a separate box, because the eye does not have to travel back and forth.

Examplepython
Open in editor »
Quick check

What must you give each line so the legend can name it?

Your turn: write the code

Draw both lines with the labels 'Pens' and 'Books', add the legend, then print the legend texts as a list (use [t.get_text() for t in ax.get_legend().get_texts()]).

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.