Labels, Titles & Legends

Matplotlib is a Python library for creating charts and visualizations — but a chart without labels is a puzzle, so this lesson is about making every plot explain itself.

Learn Labels, Titles & Legends in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll add axis labels and a title, attach a legend that names each line, and position that legend exactly where you want it.

What You'll Learn in This Lesson

1 Axis Labels and a Title

Three calls turn an anonymous chart into a clear one: plt.xlabel() , plt.ylabel() , and plt.title() . Add them any time before plt.show() .

What you'll see: a rising line, now with "Month" written under the x-axis, "Sales (units)" rotated along the y-axis, and a bold "Monthly Sales" heading centered above the chart.

2 Adding a Legend

When a chart has several lines, a legend tells the reader which is which. Give each line a label= , then call plt.legend() once.

What you'll see: two lines — Online climbing and In-store falling — with a small legend box in a corner that maps each color to its name, plus the axis labels and title from before.

3 Positioning the Legend

By default Matplotlib drops the legend wherever it fits, but you can place it precisely with the loc argument — for example 'upper left' or 'lower right' .

What you'll see: the steep "Squares" curve and the gentler "Doubles" line, with the legend box anchored firmly in the top-left corner instead of floating wherever Matplotlib chooses.

🎯 Your Turn: Fill in the Blanks

Replace each ___ to fully label this chart and show a legend.

📋 Common Errors & Quick Reference

You called plt.legend() but never passed label= to your plots. Add a label to each line.

Add your labels and legend before plt.show() . Once the figure is shown it is cleared.

Call

What It Does

plt.xlabel("...")

Label the x-axis

plt.ylabel("...")

Label the y-axis

plt.title("...")

Add a chart heading

plt.plot(x, y, label="...")

Name a line for the legend

plt.legend(loc="upper left")

Show and position the legend

🏆 Mini Challenge: A Fully Labelled Chart

Plot two lines and add axis labels, a title, and a positioned legend.

❓ Frequently Asked Questions

Lesson 4 complete — your charts explain themselves!

You added axis labels and titles, named your lines, and placed a legend exactly where you wanted it.

🚀 Up next: Markers, Line Styles & Colors — control exactly how each line looks.

Practice quiz

Which call labels the horizontal axis?

  • plt.axis('x')
  • plt.xname()
  • plt.xlabel('text')
  • plt.labelx()

Answer: plt.xlabel('text'). plt.xlabel('text') sets the label under the x-axis.

Which call labels the vertical axis?

  • plt.ylabel('text')
  • plt.axisy('text')
  • plt.labely('text')
  • plt.yname('text')

Answer: plt.ylabel('text'). plt.ylabel('text') sets the label along the y-axis.

Which call adds a heading above the chart?

  • plt.heading('text')
  • plt.caption('text')
  • plt.header('text')
  • plt.title('text')

Answer: plt.title('text'). plt.title('text') draws a heading above the plot.

How do you give a line a name for the legend?

  • plt.name('Sales')
  • Pass label='Sales' to plt.plot
  • plt.legend('Sales')
  • plt.tag('Sales')

Answer: Pass label='Sales' to plt.plot. Pass label= to each plot call, e.g. plt.plot(x, y, label='Sales').

Which call actually displays the legend box?

  • plt.showlegend()
  • plt.key()
  • plt.legend()
  • plt.labels()

Answer: plt.legend(). plt.legend() draws the legend listing every labelled line.

What does plt.legend(loc='upper left') do?

  • Pins the legend to the upper-left corner
  • Hides the legend
  • Adds a second legend
  • Removes the labels

Answer: Pins the legend to the upper-left corner. The loc argument positions the legend, here in the upper-left corner.

Why might plt.legend() show nothing and warn 'No artists with labels'?

  • The title is missing
  • The figure is too small
  • show() was already called
  • None of the plotted lines were given a label=

Answer: None of the plotted lines were given a label=. legend() only lists artists that have a label; without labels it warns and shows nothing.

What does loc='best' do?

  • Doubles the legend size
  • Lets Matplotlib pick the least-crowded corner
  • Disables the legend
  • Moves the legend off-screen

Answer: Lets Matplotlib pick the least-crowded corner. loc='best' asks Matplotlib to choose the least-crowded location automatically.

When must labels and legend be added relative to plt.show()?

  • After show()
  • It does not matter
  • Before show(), since show clears the figure
  • Only inside a loop

Answer: Before show(), since show clears the figure. Add labels and the legend before show(); once shown, the figure is cleared.

Which loc value places the legend in the bottom-right corner?

  • loc='top right'
  • loc='bottom'
  • loc='right bottom'
  • loc='lower right'

Answer: loc='lower right'. loc='lower right' anchors the legend to the bottom-right corner.

Continue this course