Monthly meals at two cafeterias, added in a loop

Write code · Medium · §10.1 Twelve months of data and graph_objects

The table canteen holds the monthly meals served (thousands) at two student cafeterias, Hall and Dorm, from March to August. Make an empty Figure with go.Figure(), and in a loop add one go.Scatter trace per cafeteria to draw a line chart with the date (date) on the x-axis and the meals (meals) on the y-axis.

Draw both lines and points, name each trace after its cafeteria, and set the line colors from cafe_colors and the line dashes from cafe_dashes. The title is Meals served per month, the x-axis title is Month, and the y-axis title is Meals (thousands). The tooltip shows both cafeterias' values in one box (hovermode="x unified"). Show the figure with fig.show(). Grading changes the meal counts and the values in the two dictionaries and draws the chart again.

Target figure for Monthly meals at two cafeterias, added in a loop
Target figure

Starter code

import pandas as pd
import plotly.graph_objects as go

dates = pd.date_range("2026-03-01", periods=6, freq="MS")
canteen = pd.DataFrame({
    "date": list(dates) * 2,
    "cafeteria": ["Hall"] * 6 + ["Dorm"] * 6,
    "meals": [42, 45, 44, 39, 12, 10, 30, 31, 33, 32, 18, 16],
})
cafe_colors = {"Hall": "#009E73", "Dorm": "#CC79A7"}
cafe_dashes = {"Hall": "solid", "Dorm": "dash"}

# 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.