A blood-donor chart that makes a new Figure inside the loop
Code review · Medium · §10.1 Twelve months of data and graph_objects
This AI draft draws the monthly blood donors at two donation centers, Station and Campus, from January to June with go.Scatter traces. It was meant to put both centers' lines in one figure, but the figure shown has only the Campus line.
Fix it by changing two lines or fewer so that both centers' lines (with points) are in one figure. Grading changes the donor counts and draws the chart again.
Code to fix
import pandas as pd
import plotly.graph_objects as go
dates = pd.date_range("2026-01-01", periods=6, freq="MS")
donors = pd.DataFrame({
"date": list(dates) * 2,
"center": ["Station"] * 6 + ["Campus"] * 6,
"donors": [310, 285, 330, 350, 340, 300, 180, 120, 260, 240, 230, 150],
})
center_colors = {"Station": "#882255", "Campus": "#44AA99"}
for center in ["Station", "Campus"]:
part = donors[donors["center"] == center]
fig = go.Figure()
fig.add_trace(go.Scatter(x=part["date"], y=part["donors"], mode="lines+markers", name=center,
line_color=center_colors[center]))
fig.update_layout(title="Blood donors per month", hovermode="x unified")
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.