Matplotlib canvas drawing
        Posted  
        
            by Morgoth
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Morgoth
        
        
        
        Published on 2010-03-27T23:28:51Z
        Indexed on 
            2010/03/27
            23:33 UTC
        
        
        Read the original article
        Hit count: 512
        
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?
© Stack Overflow or respective owner