Menu
CoddyTech

Labels and Titles

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 20 of 60.

By the end of this chapter, you will draw charts like this one:

A finished chart titled Weekly Steps: a purple line of Ana's steps with circles, green and red dots for Ben's days, a black dashed goal line at 10 and a legend

It starts with words. A bare line could be money, minutes or meters. A title and two axis labels say which:

months = [1, 2, 3, 4, 5, 6]
savings = [120, 250, 310, 480, 520, 700]
plt.plot(months, savings)
plt.title("My Savings")
plt.xlabel("Month")
plt.ylabel("Savings ($)")
plt.show()

A line chart titled My Savings that climbs from 120 to 700, with Month under the axis across and Savings ($) along the axis up

Each takes one string, so an f-string works too. Put the unit in the label, and call all three before plt.show(), which finishes the chart.

challenge icon

Challenge

Medium

A solar panel logged its power every morning hour. The starter reads the panel's name and plots its line. Finish the chart:

  1. Title it with the name, a space and Output: the name Shed gives Shed Output.
  2. Label the axis across Hour and the axis up Power (W).
  3. Show the chart.

Yours should look like this one, for a panel named Balcony:

A line chart titled Balcony Output that rises from 15 to 170 watts between hours 6 and 12, with Hour across and Power (W) up

Your program prints nothing. Coddy describes your chart in the output.

Try it yourself

import matplotlib.pyplot as plt

panel = input()
hours = [6, 7, 8, 9, 10, 11, 12]
watts = [40, 95, 160, 210, 255, 280, 290]

# Don't change the line below
plt.plot(hours, watts)

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler