Separate axis titles and ranges for the two panels of a water-use chart
Fill in the blanks · Medium · §10.2 Subplots, axes, and legends
The monthly water use (tons) of two apartment buildings, Maple and Cedar, from January to June is drawn as lines in the left panel, and the six-month totals as bars in the right panel. The two panels have values of different sizes, so each panel gets its own axis titles and ranges.
The left x-axis has the title Month and three-letter English month ticks such as Jan. The left y-axis has the title Tons per month and the range [0, 600]. The right x-axis has the title Building, and the right y-axis has the title Total tons and the range [0, 3000]. Fill in the three blanks. Grading changes the water use and draws the chart again.
Code with blanks
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
dates = pd.date_range("2026-01-01", periods=6, freq="MS")
water = pd.DataFrame({
"date": list(dates) * 2,
"building": ["Maple"] * 6 + ["Cedar"] * 6,
"tons": [410, 395, 420, 455, 470, 520, 300, 310, 290, 330, 360, 380],
})
totals = water.groupby("building", as_index=False)["tons"].sum()
building_colors = {"Maple": "#009E73", "Cedar": "#CC79A7"}
fig = make_subplots(rows=1, cols=2, subplot_titles=["Monthly use", "Six-month total"])
for building in ["Maple", "Cedar"]:
part = water[water["building"] == building]
total = totals.loc[totals["building"] == building, "tons"].iloc[0]
fig.add_trace(go.Scatter(x=part["date"], y=part["tons"], mode="lines+markers", name=building,
line_color=building_colors[building]), row=1, col=1)
fig.add_trace(go.Bar(x=[building], y=[total], name=building, marker_color=building_colors[building],
showlegend=False), row=1, col=2)
fig.update_xaxes(title_text="Month", tickformat="%b", ____)
fig.____(title_text="Tons per month", range=[0, 600], row=1, col=1)
fig.update_xaxes(title_text="Building", row=1, col=2)
fig.update_yaxes(title_text="Total tons", range=[0, 3000], ____)
fig.show()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.