JuliettVictor
JuliettVictor

Reputation: 654

Spyder: refresh existing plot window instead of opening a new one

I want to show plots in a separate window. Currently I have the IPython graphics backend set to "automatic".

When I re-run the code (or plot another figure), Spyder opens a new plot window. Is it possible to refresh the figure in the window that is already opened instead of opening a new one?

Upvotes: 1

Views: 5439

Answers (2)

daniellj
daniellj

Reputation: 1

Sure, it is possible with Spyder while in the same running kernel. Try the following example using num as parameter to plt.figure(), where num will always refer to the same figure and refresh it if already opened. Also works with plt.subplots().

import matplotlib.pyplot as plt

from scipy import *
t = linspace(0, 0.1,1000)
w = rand(1)*60*2*pi

fig = plt.figure(num=10, clear=True, figsize = [10,8])
plt.plot(t,cos(w*t))
plt.plot(t,cos(w*t-2*pi/3))
plt.plot(t,cos(w*t-4*pi/3))

Upvotes: -1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

The GUI window that opens when you call plt.show() is bound to a figure. You cannot change the figure inside it. (Well, to be precise, there might be an option of obtaining a handle from the operating system and manipulating its content, but I assume this is not worth the effort.)

Re-running the code actually means that you produce a new figure since the code does not know that it's been run before.

So, exchanging the figure or reusing the window to plot a different figure is not possible.

What is possible however is to use the figure and manipulate the figure while it's open. This is done via plt.ion(). After calling this command in IPython you can adapt the figure, e.g. adding new lines to it etc.

See this example:

enter image description here

At IN [6] the window opens and when IN [7] is executed, the figure stays open and the content changes.

Upvotes: 2

Related Questions