2026, Jan 11 09:00

How to Save Multiple Matplotlib Figures in a Loop Using savefig and Unique File Names

Learn why only the last Matplotlib figure gets saved and how to fix it. Save multiple plots in a loop with savefig, unique filenames, and reliable exports.

Saving multiple Matplotlib plots often looks straightforward, yet a small detail in where you call savefig can silently drop all but one image. If a loop renders several visuals and only one file appears on disk, the save call is likely in the wrong place.

Problem setup

The snippet below generates six visuals with distinct colors and titles. Only one image ends up saved.

# define the colors
palette_swatches = [{"Neon": "#ff0099",  "Background": "#2e002e"},
                    {"Neon": "#00ff00",  "Background": "#003300"},
                    {"Neon": "#00ccff",  "Background": "#002233"},
                    {"Neon": "#ffff00",  "Background": "#333300"},
                    {"Neon": "#ff6600",  "Background": "#331a00"},
                    {"Neon": "#ff00ff",  "Background": "#330033"}
]

# visualize networks
for ix, (metro, edge_data) in enumerate(city_map.items()):
    neon_color = palette_swatches[ix]['Neon']
    bg_color = palette_swatches[ix]['Background']
    line_w = 0.5

    fig_obj, axes = plt.subplots(1, 1, figsize=(12, 12))
    edge_data.plot(ax=axes, color=neon_color, linewidth=line_w, alpha=0.9)
    axes.set_facecolor(bg_color)

    for xt in axes.get_xticklabels(): xt.set_visible(False)
    for yt in axes.get_yticklabels(): yt.set_visible(False)
    for tl in axes.get_xticklines(): tl.set_visible(False)
    for tl in axes.get_yticklines(): tl.set_visible(False)

    y_min, y_max = plt.ylim()
    pad = 0.1 * (y_max - y_min)
    axes.set_ylim(y_min, y_max + pad)
    axes.set_title(metro, fontsize=20, color=neon_color, y=0.9)

plt.savefig("cities.png")

What’s going on

The loop creates multiple figures, but the save call happens only once after the loop completes. At that moment, the last-created figure is the one that gets written to disk, so only a single file appears. The rest were never saved because no save call was made while those figures were active.

Fix

Trigger saving for each figure inside the loop and give every file a unique name. Using the figure handle to call savefig and an index in the filename ensures every image is persisted separately.

# define the colors
palette_swatches = [{"Neon": "#ff0099",  "Background": "#2e002e"},
                    {"Neon": "#00ff00",  "Background": "#003300"},
                    {"Neon": "#00ccff",  "Background": "#002233"},
                    {"Neon": "#ffff00",  "Background": "#333300"},
                    {"Neon": "#ff6600",  "Background": "#331a00"},
                    {"Neon": "#ff00ff",  "Background": "#330033"}
]

# visualize networks and save each one separately
for ix, (metro, edge_data) in enumerate(city_map.items()):
    neon_color = palette_swatches[ix]['Neon']
    bg_color = palette_swatches[ix]['Background']
    line_w = 0.5

    fig_obj, axes = plt.subplots(1, 1, figsize=(12, 12))
    edge_data.plot(ax=axes, color=neon_color, linewidth=line_w, alpha=0.9)
    axes.set_facecolor(bg_color)

    for xt in axes.get_xticklabels(): xt.set_visible(False)
    for yt in axes.get_yticklabels(): yt.set_visible(False)
    for tl in axes.get_xticklines(): tl.set_visible(False)
    for tl in axes.get_yticklines(): tl.set_visible(False)

    y_min, y_max = plt.ylim()
    pad = 0.1 * (y_max - y_min)
    axes.set_ylim(y_min, y_max + pad)
    axes.set_title(metro, fontsize=20, color=neon_color, y=0.9)

    fig_obj.savefig(f"cities{ix}.png")

Why this matters

Batch-generating visuals is routine in data pipelines and reporting. If exports happen only once after rendering, you risk losing most of your outputs without noticing. Saving per iteration with unique filenames makes the process deterministic and avoids silent data loss.

Takeaways

When producing multiple plots in a loop, save each one right after it’s created and provide distinct filenames. This small placement change for savefig is enough to ensure every plot is written exactly once and nothing gets overwritten or skipped.