Chart chooser

Pick then draw.

Pick then draw.

Goal

Run every block and look at the Plot panel.

# Question: which city sold the most?
df = pd.DataFrame({
    "city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
    "units": [21, 15, 15, 18, 12],
    "shillings": [2100, 1500, 1480, 1750, 1190],
})
ord_ = df.sort_values("units")
plt.barh(ord_["city"], ord_["units"])
plt.title("Ranking → sorted bar")
plt.show()
# Question: how did rain change?
df = pd.DataFrame({
    "month": ["Jan", "Feb", "Mar", "Apr"] * 2,
    "mm": [48, 55, 92, 150, 22, 18, 40, 80],
    "city": ["Nairobi"] * 4 + ["Mombasa"] * 4,
})
for city in ["Nairobi", "Mombasa"]:
    sub = df[df["city"] == city]
    plt.plot(sub["month"], sub["mm"], marker="o", label=city)
plt.legend()
plt.title("Change over time → line")
plt.show()
# Question: do units track shillings?
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"],
})
plt.scatter(df["units"], df["shillings"])
plt.title("Relationship → scatter")
plt.show()
print("Ask the question, then pick the mark.")
Pitfall

Always plt.show(). Paste the whole editor.