使用matplotlib绘制图表时,GUI会冻结

问题描述 投票:0回答:1

我有一个用wxPython编写的GUI(some extra information can be found in a different question)。图形用户界面有指标(图表,文本等)和控件(按钮,放射线框等)。我经常会得到新的数据来绘制。取决于它可以的数据集的大小最多需要20秒才能生成图形并绘制它。在此期间,由于GUI线程忙于绘图,因此GUI控件无响应。

无论我正在绘制的数据集大小如何,如何使GUI控件始终响应?

python-2.7 wxpython
1个回答
0
投票

这是这个问题的解决方案。简单地说,

  1. 在一个单独的线程中绘图
  2. 将数字保存到缓冲区(带有io.Bytes()的字节流)
  3. 检索缓冲区并在GUI中显示为位图。

请参阅下面的代码。

    frame = wx.Frame.__init__(self, None, wx.ID_ANY, "", size = (1200,800))#, style= wx.SYSTEM_MENU | wx.CAPTION)
    self.panel = wx.Panel(self, wx.ID_ANY, style=wx.BORDER_THEME, size = (1200,800))

    #bmp1 = wx.Bitmap.FromRGBA(100, 100, red=255, alpha=0)
    self.bitmap1 = wx.StaticBitmap(self.panel)
    self.bitmap2 = wx.StaticBitmap(self.panel)

    sizer = wx.GridBagSizer(hgap = 0, vgap = 0)#(13, 11)
    sizer.Add(self.bitmap1, pos=(0,0),  flag = wx.ALL)#, flag=wx.TOP|wx.RIGHT) FIXIT so the sidebar is closer to the graph
    sizer.Add(self.bitmap2, pos=(1,0),  flag = wx.ALL)#,flag=wx.TOP|wx.RIGHT)


    def buf2wx (buf):
        import PIL
        image = PIL.Image.open(buf)
        width, height = image.size
        return wx.Bitmap.FromBuffer(width, height, image.tobytes())
    #access the buffer which was created in a different thread 
    #or use socket to retrieve it from a remote server or 
    #whatever you might want to do.
    buf = get_buf_from_somewhere() 

    self.bitmap1.SetBitmap(buf2wx(buf))
    self.bitmap2.SetBitmap(buf2wx(buf))



    self.panel.SetSizer(sizer)
    self.Layout()
    self.panel.Layout()
    self.Fit()

代码片段在不同的线程中运行,甚至在远程服务器上运行。此代码将生成一个绘图并将其保存在一个文件中,该文件可以通过GUI读取或传输到其他位置。

def plot():
    from matplotlib import pyplot as plt
    import io
    from numpy import random
    plt.figure()
    b = random.rand(100,)
    plt.subplot(311)
    plt.plot(b)
    b = random.rand(100,)
    plt.subplot(312)
    plt.plot(b)
    b = random.rand(100,)
    plt.subplot(313)
    plt.plot(b)
    plt.title("test")
    buf = io.BytesIO()
    plt.savefig(buf, format='jpg')
    buf.seek(0)
    return buf
© www.soinside.com 2019 - 2024. All rights reserved.