views:

293

answers:

1

Let's say I define a few functions to do certain matplotlib actions, such as

def dostuff(ax):
    ax.scatter([0.],[0.])

Now if I launch ipython, I can load these functions and start a new figure:

In [1]: import matplotlib.pyplot as mpl

In [2]: fig = mpl.figure()

In [3]: ax = fig.add_subplot(1,1,1)

In [4]: run functions # run the file with the above defined function

If I now call dostuff, then the figure does not refresh:

In [6]: dostuff(ax)

I have to then explicitly run:

In [7]: fig.canvas.draw()

To get the canvas to draw. Now I can modify dostuff to be

def dostuff(ax):
    ax.scatter([0.],[0.])
    ax.get_figure().canvas.draw()

This re-draws the canvas automatically. But now, say that I have the following code:

def dostuff1(ax):
    ax.scatter([0.],[0.])
    ax.get_figure().canvas.draw()

def dostuff2(ax):
    ax.scatter([1.],[1.])
    ax.get_figure().canvas.draw()

def doboth(ax):
    dostuff1(ax)
    dostuff2(ax)
    ax.get_figure().canvas.draw()

I can call each of these functions, and the canvas will be redrawn, but in the case of doboth(), it will get redrawn multiple times.

My question is: how could I code this, such that the canvas.draw() only gets called once? In the above example it won't change much, but in more complex cases with tens of functions that can be called individually or grouped, the repeated drawing is much more obvious, and it would be nice to be able to avoid it. I thought of using decorators, but it doesn't look as though it would be simple.

Any ideas?

+1  A: 

Why doesn't my answer to this SO question of yours about "refresh decorator" make it simple? I showed exactly what to do what you're again requesting here (by keeping a count of nestings -- incidentally, one that's also thread-safe) and you completely ignored my answer... peculiar behavior!-)

Alex Martelli
Sorry for the 'duplicate' question - I still want to explore the decorator route, but it sounded from many comments as though this would be somehow 'wrong', so wanted to see if there were other possible solutions.
astrofrog
An iPython-specific approach might be hooks, http://ipython.scipy.org/doc/manual/html/api/generated/IPython.core.hooks.html -- refresh e.g. in `pre_prompt_hook` -- but why force your code to be iPython-only?
Alex Martelli