graph_objects for several traces.
Goal
Run every block and confirm the chart is visible.
df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
fig = go.Figure()
fig.add_bar(x=df["city"], y=df["units"], name="units")
fig.add_bar(x=df["city"], y=df["shillings"] / 100, name="00s of shillings")
fig.update_layout(barmode="group", title="two traces")
fig.show()
print("two")df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar", "Apr"] * 2,
"mm": [48, 55, 92, 150, 22, 18, 40, 80],
"city": ["Nairobi"] * 4 + ["Mombasa"] * 4,
})
fig = go.Figure()
for city in ["Nairobi", "Mombasa"]:
sub = df[df["city"] == city]
fig.add_scatter(x=sub["month"], y=sub["mm"], mode="lines+markers", name=city)
fig.show()
print("lines")df = pd.DataFrame({
"units": [12, 7, 9, 4, 11, 3, 14, 8],
"shillings": [1260, 1540, 945, 880, 1155, 660, 1470, 1760],
"city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu", "Nakuru", "Eldoret"],
})
fig = px.scatter(df, x="units", y="shillings", color="city")
fig.show()
print("scatter")print("px is faster; go is more control.")