savefig and download

PNG via savefig, dpi, bbox_inches, and the download chip.

plt.savefig writes a PNG (or SVG) under /uploads. After Run, click on the file chip. You can still plt.show() in the same script.

Goal

Save a PNG with dpi and tight bounding box, then confirm it appears in /uploads.

PNG

import os

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Mombasa", "Kisumu"],
        "units": [19, 13, 14],
    }
)
sns.barplot(data=df, x="city", y="units")
plt.title("Units by city")
plt.savefig("units.png", dpi=120, bbox_inches="tight")
plt.show()
print("uploads:", os.listdir("/uploads"))

bbox_inches="tight" trims leftover margin around the title and legend.

Figure-level save

import os

df = pd.DataFrame(
    {
        "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu"] * 2,
        "product": ["A", "B"] * 6,
        "units": [12, 7, 9, 4, 11, 3, 10, 8, 6, 5, 14, 2],
        "price": [10.5, 22.0, 10.5, 22.0, 10.5, 22.0] * 2,
    }
)
df["revenue"] = df["units"] * df["price"]
g = sns.relplot(data=df, x="units", y="revenue", hue="product", col="city")
g.savefig("relplot.png", dpi=120, bbox_inches="tight")
plt.show()
print([n for n in os.listdir("/uploads") if n.endswith(".png")])

FacetGrid.savefig writes the whole grid. plt.savefig also works after relplot because it uses the current figure.

SVG

import os

df = pd.DataFrame({"city": ["A", "B", "C"], "units": [12, 7, 2]})
sns.barplot(data=df, x="city", y="units")
plt.title("units")
plt.savefig("units.svg")
plt.show()
print([n for n in os.listdir("/uploads") if n.endswith(".svg")])

SVG stays sharp when you scale it. Spreadsheets and slides more often want PNG.

Tip

dpi=120 is enough for this panel. Use 200 if you will drop the PNG into a document. Call savefig before or after show — both work here.