subplots=True.
Goal
Run every block.
df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
df.set_index("city")[["units", "shillings"]].plot(kind="bar", subplots=True, figsize=(6, 5))
plt.tight_layout()
plt.show()df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar", "Apr"] * 2,
"mm": [48, 55, 92, 150, 22, 18, 40, 80],
"city": ["Nairobi"] * 4 + ["Mombasa"] * 4,
})
df.pivot(index="month", columns="city", values="mm").plot(subplots=True, figsize=(6, 5), marker="o")
plt.tight_layout()
plt.show()df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
df.plot(x="city", y="units", kind="bar")
plt.show()print("subplots=True splits columns.")Pitfall
Always plt.show().