How do I tell matplotlib that I am done with a plot?

Question:

The following code plots to two PostScript (.ps) files, but the second one contains both lines.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")


plt.subplot(111)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")

How can I tell matplotlib to start afresh for the second plot?

Asked By: Stefano Borini

||

Answers:

You can use figure to create a new plot, for example, or use close after the first plot.

Answered By: David Cournapeau

There is a clear figure command, and it should do it for you:

plt.clf()

If you have multiple subplots in the same figure

plt.cla()

clears the current axes.

Answered By: randlet

As stated from @DavidCournapeau, use figure().

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.figure()
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")


plt.figure()
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")

Or subplot(121) / subplot(122) for the same plot, different position.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.subplot(121)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")

plt.subplot(122)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")
Answered By: lmount

Just enter plt.hold(False) before the first plt.plot, and you can stick to your original code.

Answered By: Dirklinux

If you’re using Matplotlib interactively, for example in a web application, (e.g. ipython) you maybe looking for

plt.show()

instead of plt.close() or plt.clf().

Answered By: Damo

If none of them are working then check this.. say if you have x and y arrays of data along respective axis. Then check in which cell(jupyter) you have initialized x and y to empty. This is because , maybe you are appending data to x and y without re-initializing them. So plot has old data too. So check that..

Answered By: Seeni

From the source code of matplotlib.pyplot, under the figure() documentation:

If you are creating many figures, make sure you explicitly call
    `.pyplot.close` on the figures you are not using, because this will
    enable pyplot to properly clean up the memory.

So, as others have said, use plt.close() on each figure when you’re done with it, and you’ll be good to go!

NOTE: if you create the figure via f = plt.figure(), you can close it via plt.close( f ) instead of f.close().

Answered By: jvriesem

Did you try plt.close()? that worked for me and made sure I didn’t accidentally save the same plot multiple times.

Code:

def save_to(root: Path, plot_name: str = 'plot', close: bool = True):
    """
    Assuming there is a plot in display, saves it to local users desktop users desktop as a png, svg & pdf.

    note:
        - ref on closing figs after saving: https://stackoverflow.com/questions/741877/how-do-i-tell-matplotlib-that-i-am-done-with-a-plot
        - clf vs cla https://stackoverflow.com/questions/16661790/difference-between-plt-close-and-plt-clf
    """
    root: Path = root.expanduser()
    plt.savefig(root / f'{plot_name}.png')
    plt.savefig(root / f'{plot_name}.svg')
    plt.savefig(root / f'{plot_name}.pdf')
    if close:
        # plt.clf()
        # plt.cla()
        plt.close()

in my ultimate utils lib: https://github.com/brando90/ultimate-utils

Answered By: Charlie Parker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.