Matplotlib savefig() over multiple graphs keeps saving the same graph
Asked Answered
H

4

13

So I have a function generategraph(file) which correctly creates a bar graph based on the data in the parameter and then saves it. Here is the part that saves it.

    plt.show()
    savefile = file.split('.txt')[0] + '.png'
    plt.savefig(savefile)

then in main, I will go through a set of files and call generategraph on each one.

    for fil in files:
        generategraph(fil)

plt.show() gives me the correct graphs (different graphs each time), but when I go to the saved figures they are all of the same graph (so len(files) number of saved figures but each one is the graph of the first file if that makes sense). I am just confused because plt.show() is doing what I want plt.savefig to do.

Horn answered 26/9, 2017 at 4:48 Comment(1)
return the figure from generategraph and use fig.savefig(path): for fil in files: fig = generategraph(fil); fig.savefig(fil.split('.txt')[0])Novak
S
22

You're using the state-machine (pyplot) interface. Don't.

Create your figures explicitly:

fig1, ax1 = pyplot.subplots()

Act on them directly:

lines, = ax1.plot(data1, data2, ...)

Then save and close them individually:

fig1.savefig(filename, dpi=300)
pyplot.close(fig1)
Subsidize answered 26/9, 2017 at 5:14 Comment(1)
For me, this is the correct solution , since writing plt.show after saving the images is not useful if you have dozens of itNagoya
N
4

You probably need to verify that the name of the figure you save is different for each. (The following is pseudocode, it is not clear how you get the file name.)

[edit] Then you probably should place plt.show() after plt.savefig(savefile)

#initialize idx to 0 earlier, and don't re-initialize it.

idx += 1
savefile = file + str(idx) + '.png'   # file might need to be replaced by a string
plt.savefig(savefile)
plt.show()              # place after plt.savefig()

Alternatively, you change the signature of generategraph

def generategraph(file, idx):
    savefile = file + str(idx) + '.png'  # file might need to be replaced by a string
    plt.savefig(savefile)
    plt.show()              # place after plt.savefig()



for idx, fil in enumerate(files):
    generategraph(fil, idx)
Naomanaomi answered 26/9, 2017 at 4:52 Comment(3)
Each figure is saving with a different name each time in the code that I wrote. For example if i had 3 files named "a.txt, b.txt, c.txt" my code will go through each one, generate a graph based on its data, and then save it as "a.png, b.png, c.png". This part of the code works as I end up with those .png files. The problem is that each one of those .png files is of the same graph. But the graphs that are displayed by plt.show() are the correct graphs. Im just confused because I am saving and showing them at the same point in the code. It should be the same but its not.Horn
ok, good to know - I edited my answer, suggesting to place plt.show() after plt.savefig()Naomanaomi
Perfect, placing .show() after .savefig() fixed the problem. Thanks!Horn
P
1

You can simply call plt.close() after calling plt.savefig(...).

It's the most straight forward solution that does not show a Matplotlib window that you would have to close.

Phemia answered 17/3, 2024 at 11:3 Comment(0)
S
-1

I had the exact same issue. Instead of using plt.savefig('figure name.png') just after ploting the figure, I used savefile.savefig('figure name.png'). that solved it.

Steel answered 3/8, 2023 at 14:23 Comment(2)
In the question above, savefile is a string, so it has no member function called savefig. If this solution worked for you, it may be because of a difference in your local code. If this is the case, it might be helpful to post more code to show what savefile is in your code for context.Amorette
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Amorette

© 2022 - 2025 — McMap. All rights reserved.