1

My question is kind of the 'too broad' category, but I already have been searching for hours, and my problem is still present.

I am making multiple (say 10) plots in a for cycle with my own Plotter function, and then saving them using savefig(). After that, I would like to make another plot and show it. No matter what I do, I still get 9 or 10 additional valid/empty plots.

for ...:
    pyplot = Plotter.plotStuff(data[i])
    pyplot.savefig(path)
    pyplot.clf()
pyplot.plot(new_data)
pyplot.show() # showing additional plots

My main question is: how could I "get a fresh start" and erase my previous plots? I tried pyplot.clf(), pyplot.cla() and pyplot.hold(False) with no luck.

Also, could someone please explain how the pyplot/figure/subplot dynamics work, when is it making a new plot, where is stored my data/previous plots? I had the "additional empty plot" problem before, but I reordered my commands until it worked, but I still have no clue why.

For example, I have figure(), subplots_adjust(), plot(), xticks(), yticks() commands (in this order). What should be the order and why?

Thank you very much, I'm pulling my hair out.

3
  • 1
    In this line: pyplot = Plotter.plotStuff(data[i]) you are assigning over the top of pyplot, so if you were expecting pyplot to contain the same functions as the matplotlib.pyplot module you will be disappointed. I can't say much more without knowing what sort of object Plotter.plotStuff returns. Commented May 16, 2016 at 20:33
  • @ali_m The plotStufffunction contains pyplot.figure(), ..., pyplot.yticks() what I mentioned, and then return pyplot. Actually, when I put a pyplot.close() as @cmmnn mentioned in the answer below, it is working, and it's not a problem to overdefine the pyplot module with a modified pyplot module. Still renamed it just to be sure. Commented May 16, 2016 at 21:07
  • @ali_m For the pyplot behaviour: it looks similar to that of xlwings, and you select the figure with pyplot.figure(number). Then you just call the pyplot functions like pyplot.function(), and it will make the effect on the selected figure. However, if I try to change the colour cycle, in the following way, it makes an empty figure 4, and puts the plots on figure 5: pyplot.figure(4), ax = pyplot.subplots()[1], ax.set_prop_cycle(cycler('color', ['y', 'r'])), pyplot.plot(data) Commented May 16, 2016 at 21:21

1 Answer 1

1

I am by no means an expert, but from my understanding you use figure() to open create a new figure object. You can give an argument to this function to identify your different figures i.e. figure(7), otherwise they are just enumerated I guess.

You can then put stuff into your figure object and afterwards use savefig(), draw(), show() on your figure. You can also use close() to close the last figure you created or use an identifier here again.

So for your specific problem I think a close() at the end of the for loop should do the trick.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked! However, I am still not sure about everything, see my replies to @ali_m, maybe you can help.
Glad I could be of some help and I am suprised no matplotlib expert came around here and gave a better answer. I always click around the matplotlib thumbnail gallery until I find what I need and just thought a quick working fix may be needed until a matplotlib guru comes around and enlightens us with a deeper understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.