开发者

Matplotlib canvas drawing

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 drawin开发者_如何学编程g 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?


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!-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜