清晰的图形子图 matplotlib python [英] Clear figure subplots matplotlib python

查看:64
本文介绍了清晰的图形子图 matplotlib python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的 Python 函数来生成一个 matplotlib 图.我从一个单独的脚本中多次调用 plotData ,但每次都会生成一个新图.我想要的是始终只包含一个带有 subplot.clear()之类的图,以清除数据更改之间的子图.

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.

我需要一种从 plotData 外部识别图形的方法,以便我可以清除新数据的绘图.实现这一目标的最佳方法是什么?

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?

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

推荐答案

我不确定我是否完全理解问题所在.如果要更新绘图,则需要执行此操作的函数.我会称这个函数为plotData.在此之前,您还需要设置图表.这就是您当前在 plotData 中所拥有的.因此,让我们将其重命名为 generatePlot.

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.

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)

现在您只需在开始时调用 generatePlot 一次.之后,您可以随时使用新数据更新您的绘图.

Now you need to call generatePlot only once at the beginning. Afterwards you can update your plot with new data whenever you want.

这篇关于清晰的图形子图 matplotlib python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆