A pool subplot that ties each month line and total bar to one legend item

Write code · Challenge · §10.2 Subplots, axes, and legends

The table pools holds the monthly swimmers (hundreds) at an indoor and an outdoor pool (Indoor and Outdoor) from May to October, and the table totals holds each pool's total. Use make_subplots() to put two panels side by side. In the left panel, draw each pool's swimmers (swimmers) by date (date) as a line with points using go.Scatter, and in the right panel draw one total bar per pool using go.Bar.

The panel titles are Monthly swimmers on the left and Total swimmers on the right. Name each trace after its pool, and color the lines and bars as pool_colors says. Hide the bars from the legend so that the legend has one item per pool, and group the traces so that clicking a pool in the legend hides its line and bar together. The legend title is Pool. Show the figure with fig.show(). Grading changes the swimmer counts and the color dictionary and draws the chart again.

Target figure for A pool subplot that ties each month line and total bar to one legend item
Target figure

Starter code

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

dates = pd.date_range("2026-05-01", periods=6, freq="MS")
pools = pd.DataFrame({
    "date": list(dates) * 2,
    "pool": ["Indoor"] * 6 + ["Outdoor"] * 6,
    "swimmers": [42, 40, 45, 38, 36, 44, 8, 35, 88, 95, 30, 0],
})
totals = pools.groupby("pool", as_index=False)["swimmers"].sum()
pool_colors = {"Indoor": "#56B4E9", "Outdoor": "#E69F00"}

# Write your code here

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.