在wxPython中嵌入matplotlib FuncAnimation:不需要的图弹出

时间:2018-08-27 22:53:55

标签: python matplotlib wxpython wxwidgets

我试图修改以下示例以进行实时绘图。

Embedding a matplotlib figure inside a WxPython panel

我正在尝试读取来自Arduino的串行数据并绘制/更新收集的数据。问题是该图形出现在wx App之前,我需要关闭该图形才能看到wx App。

我认为问题与以下几行有关,但我不知道为什么。

self.figure  = plt.figure(figsize=(20,20))
self.ax = plt.axes(xlim=(0, 1000), ylim=(0, 5000))

脚本如下。

import wx
from matplotlib.figure import Figure as Fig
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar

from collections import deque
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mlp
import numpy as np

# Class that inherits wx.Panel. The purpose is to embed it into 
# a wxPython App. That part can be seen in main()
class Serial_Plot(wx.Panel):
    def __init__(self, parent, strPort, id=-1, dpi=None, **kwargs):
        super().__init__(parent, id=id, **kwargs)
        self.figure  = plt.figure(figsize=(20,20))
        self.ax = plt.axes(xlim=(0, 1000), ylim=(0, 5000))
        self.plot_data, = self.ax.plot([], [])
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.toolbar = NavigationToolbar(self.canvas)
        self.toolbar.Realize()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.RIGHT | wx.EXPAND)
        self.SetSizer(sizer)

        # Serial communication
        self.ser = serial.Serial(strPort, 115200)
        # Serial data initialized as deque. The serial readings from arduino
        # are set to be one value per line.
        self.vals = deque()
        # matplotlib function animation
        anim = animation.FuncAnimation(self.figure, self.update, 
                                   interval=20)
        plt.show()
        self.close
    def update(self, i):
        try:
            # read serial line
            data = float(self.ser.readline().decode('utf-8'))
            self.vals.append(data)
            # update plot data
            self.plot_data.set_data(range(len(self.vals)), self.vals)
        except:
            pass
        return self.plot_data

    def close(self):
        # close serial
        self.ser.flush()
        self.ser.close()

def main():
    app = wx.App(False)
    frame = wx.Frame(None, -1, "WX APP!")
    demo_plot = Serial_Plot(frame,'COM3')
    frame.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

以下是在GUI之前弹出的图形。

enter image description here

在关闭图形后,wx应用程序可见。

enter image description here

我试图摆脱弹出的图形,只看到嵌入在wx应用程序中的图形。我真的很感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为您可能通过使用animation.FuncAnimation来解决错误的问题,因为我认为这是一个matplotlib函数,预计将由matplotlib的主循环控制,但您使用的是wxpython有它自己的。 (在这一点上,我保留严重错误的权利:))
下面是您的代码,经过重新设计以使用random来避免串行端口,并包含一个wx.Timer来执行更新。

import wx
from matplotlib.figure import Figure as Fig
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar

from collections import deque
#import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mlp
import numpy as np
import random

# Class that inherits wx.Panel. The purpose is to embed it into
# a wxPython App. That part can be seen in main()
class Serial_Plot(wx.Panel):
    def __init__(self, parent, strPort, id=-1, dpi=None, **kwargs):
        super().__init__(parent, id=id, **kwargs)
        self.figure  = plt.figure(figsize=(20,20))
        self.ax = plt.axes(xlim=(0, 10), ylim=(0, 50))
        self.plot_data, = self.ax.plot([], [])
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.toolbar = NavigationToolbar(self.canvas)
        self.toolbar.Realize()
#
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
#
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.RIGHT | wx.EXPAND)
        self.SetSizer(sizer)

        # Serial communication
        # self.ser = serial.Serial(strPort, 115200)
        # Serial data initialized as deque. The serial readings from arduino
        # are set to be one value per line.
        self.vals = deque()
        # matplotlib function animation
        #anim = animation.FuncAnimation(self.figure, self.update,
        #                           interval=2)
        #plt.show()

        plt.ion() #Turn on interactive plot

        #self.close
#
        self.timer.Start(1000)

    def update(self,event):
        #try:
            # read serial line
            #data = float(self.ser.readline().decode('utf-8'))
        data = float(random.randint(1, 50))
        self.vals.append(data)
            # update plot data
        length = len(self.vals)
        self.plot_data.set_data(range(length), self.vals)

        #Update x axis to follow interactive plot
        self.ax.set_xlim(0.0,float(length + 1))

        #except:
        #    pass
        #return self.plot_data
        plt.plot()

    def close(self):
        # close serial
        self.ser.flush()
        self.ser.close()

def main():
    app = wx.App(False)
    frame = wx.Frame(None, -1, "WX APP!")
    demo_plot = Serial_Plot(frame,'COM3')
    frame.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

enter image description here

N.B。
可以将self.ax.set_xlim(0.0,float(length + 1))调整为self.ax.set_xlim(float(length - 10), float(length + 1))之类的值,使其遵循当前值,而不仅仅是不断地扩展x轴。