Menu
CoddyTech

Several Charts in One

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

A food truck tracks its customers and their wait. Different units read best as two charts, side by side in one figure:

Two charts side by side: bars of customers from Monday to Friday, rising to 150 on Friday, and a line of the wait in minutes, rising to 12 on Friday

Busy days, long waits: one glance tells the story. plt.subplots(1, 2) makes 1 row of 2 charts, axs[0] on the left and axs[1] on the right:

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
customers = [60, 85, 70, 120, 150]
wait = [4, 6, 5, 9, 12]
fig, axs = plt.subplots(1, 2)
axs[0].bar(days, customers)
axs[0].set_title("Customers")
axs[1].plot(days, wait)
axs[1].set_title("Wait (min)")
plt.show()

Each one is a full chart with the methods you know. Rows come first, like a shape: plt.subplots(2, 1) stacks them.

challenge icon

Challenge

Medium

A hiker logged eight hikes: km walked and hours taken. The starter's label(ax, title, xlabel, ylabel) names one chart. Draw two charts side by side:

  1. Left: a line of km (up) per hike number in hikes (across).
  2. Right: one dot per hike, distance across and time up.
  3. Name each chart with one label call, using the texts below.

Yours should look like this one, from another hiker's log:

Two charts side by side: left, a line titled Distance per Hike with Hike across and Distance (km) up; right, dots titled Time vs Distance with Distance (km) across and Time (h) up

The texts: Distance per Hike, Hike, Distance (km) on the left; Time vs Distance, Distance (km), Time (h) on the right. Your program prints nothing.

Try it yourself

import numpy as np
import matplotlib.pyplot as plt

hikes = np.arange(1, 9)
km = np.array([8.0, 12.5, 6.0, 15.0, 10.0, 18.5, 9.5, 14.0])
hours = np.array([2.5, 4.0, 2.0, 5.0, 3.5, 6.5, 3.0, 4.5])


# Gives one chart its title and axis labels (returns nothing)
def label(ax, title, xlabel, ylabel):
    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)


# Write your code below

# Don't change the line below
plt.show()
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