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:
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
MediumA 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:
- Left: a line of
km(up) per hike number inhikes(across). - Right: one dot per hike, distance across and time up.
- Name each chart with one
labelcall, using the texts below.
Yours should look like this one, from another hiker's log:
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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler