close a figure after show , when plotting many figures from script- using matplotlib.pyplot.figure

Hi

I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.

I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up…the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side…while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously…but I dont know how to achieve this.

Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks

Hari

from matplotlib.pyplot import figure

def do_my_plot(well_id):

processed_data_object = processed_dict[well_id]

fig = figure(figsize=(7,7)

ax = fig.add_subplot(1,1,1)

par1 =ax.twinx()

par2 = ax.twinx()

Plot all the data

par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).

par2.plot(…

finally

fig.show()

I tried fig.clf()

def plot_and_process_data():

for well_id in list_of_384_well_ids:

process_data(well_id)

do_my_plot(well_id)

Hari,

You can give a number to figure(), as in figure(1), and it will reuse figure 1. Also, you can close figure 1 with pyplot.close(1).

-Sterling

···

On Oct 16, 2012, at 8:25AM, hari jayaram wrote:

Hi
I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.
I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up..the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side..while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously..but I dont know how to achieve this.
Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks
Hari

from matplotlib.pyplot import figure

def do_my_plot(well_id):
    processed_data_object = processed_dict[well_id]
    fig = figure(figsize=(7,7)
    ax = fig.add_subplot(1,1,1)
    par1 =ax.twinx()
    par2 = ax.twinx()
    # Plot all the data
    par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).
    par2.plot(....
    # finally
    fig.show()
    # I tried fig.clf()

def plot_and_process_data():
    for well_id in list_of_384_well_ids:
         process_data(well_id)
         do_my_plot(well_id)

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Or you can call ax.cla() to clear the axes before plotting the next
data set. Then subsequent calls to plot don't need 300+ figure
objects.

···

On Tue, Oct 16, 2012 at 5:09 PM, Sterling Smith <smithsp@...3304...> wrote:

Hari,

You can give a number to figure(), as in figure(1), and it will reuse figure 1. Also, you can close figure 1 with pyplot.close(1).

-Sterling

On Oct 16, 2012, at 8:25AM, hari jayaram wrote:

Hi
I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.
I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up..the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side..while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously..but I dont know how to achieve this.
Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks
Hari

from matplotlib.pyplot import figure

def do_my_plot(well_id):
    processed_data_object = processed_dict[well_id]
    fig = figure(figsize=(7,7)
    ax = fig.add_subplot(1,1,1)
    par1 =ax.twinx()
    par2 = ax.twinx()
    # Plot all the data
    par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).
    par2.plot(....
    # finally
    fig.show()
    # I tried fig.clf()

def plot_and_process_data():
    for well_id in list_of_384_well_ids:
         process_data(well_id)
         do_my_plot(well_id)

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

Hari,

To recycle the figure, try the following:

import matplotlib.pyplot as plt

def do_my_plot(par1, par2, well_id):
processed_data_object = processed_dict[well_id]

Plot all the data

par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).

par2.plot(…

finally

plt.show()

I tried fig.clf()

def plot_and_process_data():

plt.ion() # Turn on interactive mode
fig = plt.figure(figsize=(7,7)

ax = fig.add_subplot(1,1,1)

par1 =ax.twinx()

par2 = ax.twinx()

for well_id in list_of_384_well_ids:
par1.cla()

     par2.cla()

process_data(well_id)

do_my_plot(par1, par2, well_id)

Note, this is completely untested, but it would be how I would go about it at first. The “plt.ion()” turns on interactive mode to allow your code to continue running even after the plot window appears (but does not end until the last window is closed.). Of course, another approach would simply be to do “fig.savefig()” after every update to the figure and never use show() and ion() (essentially, a non-interactive head-less script).

Hopefully, this helps.
Ben Root

···

On Tue, Oct 16, 2012 at 11:25 AM, hari jayaram <harijay@…287…> wrote:

Hi
I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.

I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up…the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side…while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously…but I dont know how to achieve this.

Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks

Hari

Thanks Benjamin, Sterling and Damon for your prompt help

However I am still not able to achieve what I wanted .

I can get the headless script to work just great where it saves all the figures and I can view them after the script is done running.

But somehow when I try the figure number method that Sterling suggested , along with the axis clear and redraw method (Damon) , or the decouple and clear and then plot method (Benjamin Root) : I get the plot just spinning with a blue circle on Windows 7 and the script just chugs merrily along.

I think part of the problem was that I was wrong in the way I stated my application. Each of the 384 data processing steps takes a few seconds…and not a minute as I had indicated. I tried with both ion() and ioff() and giving the figure a number , which stays constant and clearing the axis everytime before plotting. But I get a spiining blue circle in Windows.

I will try and cookup a test case , and send to the list , to reproduce what I am seeing. it may still be that I am calling pylab , pyplot incorrectly and hence not getting the continuously changing figure that your suggestions should give me.

hari

Using plt.ion() or plt.ioff() causes a spinning blue-ball on windows…while the rest of the script continues.

If I use the figure number trick. I get the first figure displayed.

···

On Tue, Oct 16, 2012 at 12:15 PM, Benjamin Root <ben.root@…1304…> wrote:

On Tue, Oct 16, 2012 at 11:25 AM, hari jayaram <harijay@…287…> wrote:

Hi
I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.

I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up…the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side…while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously…but I dont know how to achieve this.

Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks

Hari

Hari,

To recycle the figure, try the following:

import matplotlib.pyplot as plt

def do_my_plot(par1, par2, well_id):
processed_data_object = processed_dict[well_id]

Plot all the data

par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).

par2.plot(…

finally

plt.show()

I tried fig.clf()

def plot_and_process_data():

plt.ion() # Turn on interactive mode
fig = plt.figure(figsize=(7,7)

ax = fig.add_subplot(1,1,1)

par1 =ax.twinx()

par2 = ax.twinx()

for well_id in list_of_384_well_ids:
par1.cla()

     par2.cla()

process_data(well_id)

do_my_plot(par1, par2, well_id)

Note, this is completely untested, but it would be how I would go about it at first. The “plt.ion()” turns on interactive mode to allow your code to continue running even after the plot window appears (but does not end until the last window is closed.). Of course, another approach would simply be to do “fig.savefig()” after every update to the figure and never use show() and ion() (essentially, a non-interactive head-less script).

Hopefully, this helps.
Ben Root

Hari,

While I am not intimately acquainted with the inner working of the interactive matplotlib functionality, I have seen that it tries to not update the figure if you ask for some change to it while it is trying to update the figure. That sounds circular, but oh well.

Perhaps you could have each analysis open a new figure, and have an if statement to close 5 (or 10...) figures ago.

Another subtlety that I have noticed (and perhaps read somewhere) is that there could be a difference in behavior between having interactivity set in the matplotlibrc file and using the ion() call after having set interactive: False in the matplotlibrc file.

Another solution might be a time.sleep after each update of the figure.

(Note that with ion(), the command for updating the figure is pylab.draw, which may need to be issued after each case - the pylab/pyplot functions usually have a draw_if_interactive call in them.)

-Sterling

PS If I am causing more confusion than help, please let me know.

···

On Oct 17, 2012, at 10:54AM, hari jayaram wrote:

Thanks Benjamin, Sterling and Damon for your prompt help

However I am still not able to achieve what I wanted .

I can get the headless script to work just great where it saves all the figures and I can view them after the script is done running.

But somehow when I try the figure number method that Sterling suggested , along with the axis clear and redraw method (Damon) , or the decouple and clear and then plot method (Benjamin Root) : I get the plot just spinning with a blue circle on Windows 7 and the script just chugs merrily along.

I think part of the problem was that I was wrong in the way I stated my application. Each of the 384 data processing steps takes a few seconds..and not a minute as I had indicated. I tried with both ion() and ioff() and giving the figure a number , which stays constant and clearing the axis everytime before plotting. But I get a spiining blue circle in Windows.

I will try and cookup a test case , and send to the list , to reproduce what I am seeing. it may still be that I am calling pylab , pyplot incorrectly and hence not getting the continuously changing figure that your suggestions should give me.

hari

Using plt.ion() or plt.ioff() causes a spinning blue-ball on windows..while the rest of the script continues.
If I use the figure number trick. I get the first figure displayed.

On Tue, Oct 16, 2012 at 12:15 PM, Benjamin Root <ben.root@...1304...> wrote:

On Tue, Oct 16, 2012 at 11:25 AM, hari jayaram <harijay@...287...> wrote:
Hi
I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.
I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up..the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side..while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously..but I dont know how to achieve this.
Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks
Hari

Hari,

To recycle the figure, try the following:

import matplotlib.pyplot as plt

def do_my_plot(par1, par2, well_id):
    processed_data_object = processed_dict[well_id]
    # Plot all the data
    par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).
    par2.plot(....
    # finally
    plt.show()
    # I tried fig.clf()

def plot_and_process_data():
    plt.ion() # Turn on interactive mode
    fig = plt.figure(figsize=(7,7)
    ax = fig.add_subplot(1,1,1)
    par1 =ax.twinx()
    par2 = ax.twinx()

    for well_id in list_of_384_well_ids:
         par1.cla()
         par2.cla()
         process_data(well_id)
         do_my_plot(par1, par2, well_id)

Note, this is completely untested, but it would be how I would go about it at first. The "plt.ion()" turns on interactive mode to allow your code to continue running even after the plot window appears (but does not end until the last window is closed.). Of course, another approach would simply be to do "fig.savefig()" after every update to the figure and never use show() and ion() (essentially, a non-interactive head-less script).

Hopefully, this helps.
Ben Root

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Sterling ,

Thanks for your email. I definitely think I was running into issues with the figure updating while it was trying to draw , constantly. I experimented with sleep …but didnt try hard enough to get it to work.

That said, I have a very nice solution to my problem using the wx.aui.AuiNotebook borrowed from example wx5 (http://matplotlib.org/examples/user_interfaces/embedding_in_wx5.html)

MY SOLUTION

I now have a single Plot wx.Panel which then encloses a wx.aui.Notebook just like in the wx5 example link above.

Once I have finished processing the data . I initialize the plotter just like in the example above. I have a for loop that goes over all 384 data sets and then adds a page to the Notebook for every dataset.

The result is a 384 , tabbed Notebook , where each tab is a fully interactive matplotlib plot , created like before.

def do_my_plot(well_id, plotter_from_main):

my_plotter = plotter_from_main

Here is where I add a page to the Notebook and get its current axis

ax = plotter.add(“figure %s” % well_id).gca()

par1 = ax .twinx()

par2 = ax.twinx()

…and -so on

The neatest thing of the final Notebook is that , although it takes about a minute to appear at first call to frame.Show()…

After that it is very performant.

Importantly I can quickly scroll through all the 384 tabs ( or pages in the notebook) using CTRL-TAB to go forward and CTRL-Shift_TAB to go backward. This ability to navigate is a huge plus for me. The Navigation is actually very fast and “redraws” are nearly instantaneous.

I will still try and get the "single figure that refreshed " approach as I originally wanted, using the techniques you , Ben and Damon suggested . In many cases I dont need to have a 384 tabbed frame…and can do with a plot that refreshes every few seconds so I know that everything went right.

Thanks everyone for all your help…Ill get back to the group once I get the repaint within a single frame to work…

Hari

···

On Wed, Oct 17, 2012 at 5:38 PM, Sterling Smith <smithsp@…3304…> wrote:

Hari,

While I am not intimately acquainted with the inner working of the interactive matplotlib functionality, I have seen that it tries to not update the figure if you ask for some change to it while it is trying to update the figure. That sounds circular, but oh well.

Perhaps you could have each analysis open a new figure, and have an if statement to close 5 (or 10…) figures ago.

Another subtlety that I have noticed (and perhaps read somewhere) is that there could be a difference in behavior between having interactivity set in the matplotlibrc file and using the ion() call after having set interactive: False in the matplotlibrc file.

Another solution might be a time.sleep after each update of the figure.

(Note that with ion(), the command for updating the figure is pylab.draw, which may need to be issued after each case - the pylab/pyplot functions usually have a draw_if_interactive call in them.)

-Sterling

PS If I am causing more confusion than help, please let me know.

On Oct 17, 2012, at 10:54AM, hari jayaram wrote:

Thanks Benjamin, Sterling and Damon for your prompt help

However I am still not able to achieve what I wanted .

I can get the headless script to work just great where it saves all the figures and I can view them after the script is done running.

But somehow when I try the figure number method that Sterling suggested , along with the axis clear and redraw method (Damon) , or the decouple and clear and then plot method (Benjamin Root) : I get the plot just spinning with a blue circle on Windows 7 and the script just chugs merrily along.

I think part of the problem was that I was wrong in the way I stated my application. Each of the 384 data processing steps takes a few seconds…and not a minute as I had indicated. I tried with both ion() and ioff() and giving the figure a number , which stays constant and clearing the axis everytime before plotting. But I get a spiining blue circle in Windows.

I will try and cookup a test case , and send to the list , to reproduce what I am seeing. it may still be that I am calling pylab , pyplot incorrectly and hence not getting the continuously changing figure that your suggestions should give me.

hari

Using plt.ion() or plt.ioff() causes a spinning blue-ball on windows…while the rest of the script continues.

If I use the figure number trick. I get the first figure displayed.

On Tue, Oct 16, 2012 at 12:15 PM, Benjamin Root <ben.root@…1304…> wrote:

On Tue, Oct 16, 2012 at 11:25 AM, hari jayaram <harijay@…287…> wrote:

Hi

I am a relative newbie to matplotlib.

I have a python script that handles a dataset that comprises 384 sets of data.

At the present moment , I read in a set of data - process it - and the create a figure using code shown below.

I am using windows with the default backend ( I think I set it to wx).

When I run the program, figure after figure shows up…the program continues from well to well plotting the figure. I can close the figure window using the X on the right -hand side…while the program chugs along.

Is there a way to just recycle the figure object , so that the plot shows up for a brief second and refreshes when the next calculation is complete. Each process_data function , takes a few minutes.

Alternatively I just want to close the figure object I show after a brief lag. I am OK if that happens instantaneously…but I dont know how to achieve this.

Do I have to use the matplotlib.Figure object to achieve this functionality

Thanks

Hari

Hari,

To recycle the figure, try the following:

import matplotlib.pyplot as plt

def do_my_plot(par1, par2, well_id):

processed_data_object = processed_dict[well_id]
# Plot all the data
par1.plot(processed_data_object.raw_x,processed_data_object.raw_y).
par2.plot(....
# finally
plt.show()
# I tried  fig.clf()

def plot_and_process_data():

plt.ion()  # Turn on interactive mode
fig = plt.figure(figsize=(7,7)
ax = fig.add_subplot(1,1,1)
par1 =ax.twinx()
par2 = ax.twinx()
for well_id in list_of_384_well_ids:
     par1.cla()
     par2.cla()
     process_data(well_id)
     do_my_plot(par1, par2, well_id)

Note, this is completely untested, but it would be how I would go about it at first. The “plt.ion()” turns on interactive mode to allow your code to continue running even after the plot window appears (but does not end until the last window is closed.). Of course, another approach would simply be to do “fig.savefig()” after every update to the figure and never use show() and ion() (essentially, a non-interactive head-less script).

Hopefully, this helps.

Ben Root


Everyone hates slow websites. So do we.

Make your web apps faster with AppDynamics

Download AppDynamics Lite for free today:

http://p.sf.net/sfu/appdyn_sfd2d_oct_______________________________________________
Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users