A ski-pass y-axis setting that lands on both panels
Code review · Medium · §10.2 Subplots, axes, and legends
This AI draft draws the monthly lift passes sold (thousands) at two ski resorts, Alpine and Valley, from December to March as lines in the left panel, and the season totals as bars in the right panel. The left y-axis should have the title Passes (thousands) and the range [0, 60], and the right y-axis the title Season total (thousands) and the range [0, 200].
In the figure shown, however, both y-axes have the title Passes (thousands), and the bars on the right are cut off at 60. Change one line to set the y-axes of the two panels right. Grading changes the pass sales and draws the chart again.
Code to fix
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
dates = pd.date_range("2025-12-01", periods=4, freq="MS")
passes = pd.DataFrame({
"date": list(dates) * 2,
"resort": ["Alpine"] * 4 + ["Valley"] * 4,
"passes": [38, 52, 47, 21, 25, 41, 36, 12],
})
totals = passes.groupby("resort", as_index=False)["passes"].sum()
resort_colors = {"Alpine": "#882255", "Valley": "#44AA99"}
fig = make_subplots(rows=1, cols=2, subplot_titles=["Passes per month", "Season total"])
for resort in ["Alpine", "Valley"]:
part = passes[passes["resort"] == resort]
total = totals.loc[totals["resort"] == resort, "passes"].iloc[0]
fig.add_trace(go.Scatter(x=part["date"], y=part["passes"], mode="lines+markers", name=resort,
line_color=resort_colors[resort], legendgroup=resort), row=1, col=1)
fig.add_trace(go.Bar(x=[resort], y=[total], name=resort, marker_color=resort_colors[resort],
legendgroup=resort, showlegend=False), row=1, col=2)
fig.update_yaxes(title_text="Season total (thousands)", range=[0, 200], row=1, col=2)
fig.update_yaxes(title_text="Passes (thousands)", range=[0, 60])
fig.update_layout(title="Ski lift passes", legend_title_text="Resort")
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.