Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

22.3 savefig ने charts जतन करणं

By Ravindra Bagale

या course चे सर्व chapters (py + ch)

हे का शिकायचं?

Screenshot म्हणजे blurry आणि असंगत. Code मधून PNG काढा.

हे काय आहे?

matplotlib/seaborn plot नंतर plt.savefig('figures/....png', dpi=150, bbox_inches='tight').

कसं करायचं?

Step 1. figures/ folder तयार करा.

Step 2. Chart काढा.

Step 3. savefig dpi=150.

Step 4. Notebook मध्ये फक्त दाखवणं आणि file वेगळं ठेवणं.

उदाहरणं: copy करा आणि चालवा

उदाहरण 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")

उदाहरण 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)

उदाहरण 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.