清晰的图形子图 matplotlib python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42011587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear figure subplots matplotlib python
提问by Spencer H
I wrote a simple Python function to generate a matplotlib figure. I call plotData
multiple times from a separate script, but each time it generates a new plot. What I would like is to always have just one plot with something like subplot.clear()
to clear the subplots between data changes.
我编写了一个简单的 Python 函数来生成一个 matplotlib 图。我plotData
从一个单独的脚本中多次调用,但每次都会生成一个新图。我想要的是始终只有一个图,类似于subplot.clear()
清除数据更改之间的子图。
I need a way to identify the figure from outside plotData
so that I can clear the plots for new data. What would be the best way to accomplish this?
我需要一种从外部识别图形的方法,plotData
以便我可以清除新数据的绘图。实现这一目标的最佳方法是什么?
## Plot Data Function
def plotData(self):
# Setup figure to hold subplots
f = Figure(figsize=(10,8), dpi=100)
# Setup subplots
subplot1=f.add_subplot(2,1,1)
subplot2=f.add_subplot(2,1,2)
# Show plots
dataPlot = FigureCanvasTkAgg(f, master=app)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)
回答by ImportanceOfBeingErnest
I'm not sure if I fully understand where the problem lies.
If you want to update the plot you would need a function that does this. I would call this function plotData
. Before that you also need to set the plot up. That is what you currently have in plotData
. So let's rename that to generatePlot
.
我不确定我是否完全理解问题所在。如果要更新绘图,则需要一个执行此操作的函数。我会调用这个函数plotData
。在此之前,您还需要设置绘图。这就是您目前在plotData
. 因此,让我们将其重命名为generatePlot
.
class SomeClass():
...
def generatePlot(self):
# Setup figure to hold subplots
f = Figure(figsize=(10,8), dpi=100)
# Setup subplots
self.subplot1=f.add_subplot(2,1,1)
self.subplot2=f.add_subplot(2,1,2)
# Show plots
dataPlot = FigureCanvasTkAgg(f, master=app)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)
## Plot Data Function
def plotData(self, data, otherdata):
#clear subplots
self.subplot1.cla()
self.subplot2.cla()
#plot new data to the same axes
self.subplot1.plot(data)
self.subplot2.plot(otherdata)
Now you need to call generatePlot
only once at the beginning. Afterwards you can update your plot with new data whenever you want.
现在你generatePlot
只需要在开始时调用一次。之后,您可以随时使用新数据更新您的绘图。
回答by Nick Hale
you can use
您可以使用
subplot.cla() # which clears data but not axes
subplot.clf() # which clears data and axes