Save the figure

savefig.

savefig.

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.plot(x="city", y="units", kind="bar", title="Units")
plt.savefig("pandas_units.png", dpi=120, bbox_inches="tight")
plt.show()
print("wrote pandas_units.png")
df = pd.DataFrame({
    "month": ["Jan", "Feb", "Mar", "Apr"] * 2,
    "mm": [48, 55, 92, 150, 22, 18, 40, 80],
    "city": ["Nairobi"] * 4 + ["Mombasa"] * 4,
})
df[df.city=="Nairobi"].plot(x="month", y="mm", marker="o")
plt.savefig("pandas_rain.png", dpi=120, bbox_inches="tight")
plt.show()
print("wrote pandas_rain.png")
Pitfall

Always plt.show().