简体   繁体   English

重新配置matplotlib之前清除图形

[英]Clearing graph before replotting matplotlib

I have a little app that allows me to change an input value with a tKinter scale widget and see how a graph reacts to different changes in inputs. 我有一个小应用程序,它允许我使用tKinter比例小部件更改输入值,并查看图形如何响应输入中的不同更改。 Every time I move the scale, it's bound to an event that redoes the calculations for a list and replots. 每次移动秤时,它都会绑定到一个事件,该事件将重做列表和重绘的计算。 It's kind of slow. 有点慢。

Now, I'm replotting the entire thing, but it's stacking one axis on top of the other, hundreds after a few minutes of use. 现在,我将重新绘制整个内容,但是使用几分钟后,它会将一个轴堆叠在另一个轴上。

deltaPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
c = deltaPlot.add_subplot(111)
c.set_title('Delta')
deltaDataPlot = FigureCanvasTkAgg(deltaPlot, master=master)
deltaDataPlot.get_tk_widget().grid(row=0,rowspan=2)

and the main loop runs 和主循环运行

c.cla()
c.plot(timeSpread,tdeltas,'g-')
deltaDataPlot.show()

It's clearing the initial plot, but like I said the axes are stacking (because it's redrawing one each time, corresponding to the slightly altered data points). 它正在清除初始图,但是就像我说的那样,轴正在堆叠(因为它每次都重绘一个轴,对应于稍微改变的数据点)。 Anyone know a fix? 有人知道解决方法吗?

To improve speed there are a couple of things you could do: 要提高速度,您可以做几件事:

Either Run the remove method on the line produced by plot: 任一运行remove上由曲线所产生的线的方法:

# inside the loop
line, = c.plot(timeSpread,tdeltas,'g-')
deltaDataPlot.show()
...
line.remove()

Or Re-use the line, updating its coordinates appropriately: 重新使用该线,适当地更新其坐标:

# outside the loop
line, = c.plot(timeSpread,tdeltas,'g-')

# inside the loop
deltaDataPlot.show()
line.set_data(timeSpread,tdeltas)

The documentation of Line2d can be found here . Line2d的文档可以在这里找到。

You might also like to read the cookbook article on animation . 您可能还想阅读有关动画食谱文章

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM