22. Exporting and Sharing Your Analysis
22.3 Saving Charts with savefig
Why. A chart inside Jupyter is not a file. WhatsApp and email need PNG (or JPEG). savefig writes that file. No PDF links on the course site — and you do not need PDF for this habit.
What.
- Create
fig, ax = plt.subplots(...). - Draw with matplotlib or seaborn on
ax. fig.savefig("figures/name.png", dpi=120, bbox_inches="tight").plt.close(fig)to free memory in long notebooks.
How.
Shareable chart checklist
Path("figures").mkdir(exist_ok=True).- Clear title: “Pune delivered GMV by day (fictional)”.
- Axis labels with units (₹, orders, minutes).
- Source footnote in the title or a text box: “Sample learning data”.
dpi=120or150for phones;bbox_inches="tight"avoids cut labels.- Open the PNG from disk once before sending.
Worked example A — Bar chart PNG.
import matplotlib.pyplot as plt
from pathlib import Path
Path("figures").mkdir(exist_ok=True)
delivered = df.loc[df["status"].eq("Delivered")]
city_gmv = delivered.groupby("city")["amount"].sum().sort_values(ascending=False)
fig, ax = plt.subplots(figsize=(8, 4))
city_gmv.plot(kind="bar", ax=ax, color="#2563eb", edgecolor="black")
ax.set_ylabel("GMV (₹)")
ax.set_xlabel("City")
ax.set_title("Delivered GMV by city — fictional Blinkit / Amazon Now sample")
fig.savefig("figures/city_gmv.png", dpi=120, bbox_inches="tight")
plt.close(fig)
print("wrote figures/city_gmv.png")
Worked example B — Seaborn + savefig.
import seaborn as sns
fig, ax = plt.subplots(figsize=(7, 4))
sns.barplot(data=summary, x="city", y="gmv_inr", hue="status", ax=ax)
ax.set_title("GMV by city and status (fictional)")
ax.set_ylabel("GMV (₹)")
ax.tick_params(axis="x", rotation=30)
fig.savefig("figures/city_status_gmv.png", dpi=120, bbox_inches="tight")
plt.close(fig)
Worked example C — Phone-friendly size.
fig, ax = plt.subplots(figsize=(6, 4)) # taller than wide for status cards
ax.plot([1, 2, 3, 4], [120, 150, 180, 210], marker="o")
ax.set_title("Kolhapur fictional daily delivered orders")
ax.set_xlabel("Day in sample week")
ax.set_ylabel("Orders")
fig.savefig("figures/kolhapur_trend.png", dpi=150, bbox_inches="tight")
plt.close(fig)
Rani sends kolhapur_trend.png to the Kolhapur group with one caption line — not the notebook.
Ravindra Bagale's Tip
Title madhe 'fictional' / 'sample' liha jab learning data asel. Professional habit + honest.