在 wxPython 位图中显示 matplotlib 图形
Displaying a matplotlib graph in a wxPython bitmap
我正在尝试从 matplotlib 生成图形,将其存储在缓冲区中,然后将其设置为 wxPython 中的位图 window。
以下代码显示了我正在尝试做的事情。如果我用从磁盘加载的 PNG 替换缓冲区存储的图像,程序将按预期执行。
import io
import wx
import numpy as np
import matplotlib.pyplot as plt
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.SetSize((1024,768))
self.SetBackgroundColour('yellow')
#Generate Sample Graph
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
#Save into Buffer
buf = io.BytesIO()
plt.savefig(buf,format='png')
self.Image = wx.Image(buf, wx.BITMAP_TYPE_ANY)
self.Image = wx.StaticBitmap(self, wx.ID_ANY,
wx.Bitmap(self.Image))
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.Image,1,wx.ALIGN_CENTRE)
self.SetSizer(self.sizer)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Graph to Image Test", size=(1024,768))
self.panel = MainPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
我收到以下错误消息并且程序停止:
Traceback (most recent call last):
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 42, in <module>
frame = MyForm()
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 37, in __init__
self.panel = MainPanel(self)
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 28, in __init__
wx.Bitmap(self.Image))
wx._core.wxAssertionError: C++ assertion "image.IsOk()" failed at /home/vagrant/wxPython-4.0.4/ext/wxWidgets/src/gtk/bitmap.cpp(581) in wxBitmap(): invalid image
您需要re-set缓冲区内的位置回零,以便wx.Image
从数据的开头读取,而不是结尾。
#Save into Buffer
buf = io.BytesIO()
plt.savefig(buf,format='png')
buf.seek(0)
我正在尝试从 matplotlib 生成图形,将其存储在缓冲区中,然后将其设置为 wxPython 中的位图 window。
以下代码显示了我正在尝试做的事情。如果我用从磁盘加载的 PNG 替换缓冲区存储的图像,程序将按预期执行。
import io
import wx
import numpy as np
import matplotlib.pyplot as plt
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.SetSize((1024,768))
self.SetBackgroundColour('yellow')
#Generate Sample Graph
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
#Save into Buffer
buf = io.BytesIO()
plt.savefig(buf,format='png')
self.Image = wx.Image(buf, wx.BITMAP_TYPE_ANY)
self.Image = wx.StaticBitmap(self, wx.ID_ANY,
wx.Bitmap(self.Image))
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.Image,1,wx.ALIGN_CENTRE)
self.SetSizer(self.sizer)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Graph to Image Test", size=(1024,768))
self.panel = MainPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
我收到以下错误消息并且程序停止:
Traceback (most recent call last):
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 42, in <module>
frame = MyForm()
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 37, in __init__
self.panel = MainPanel(self)
File "/home/mike/Projects/Python/wxerg/wx + matplotlib test.py", line 28, in __init__
wx.Bitmap(self.Image))
wx._core.wxAssertionError: C++ assertion "image.IsOk()" failed at /home/vagrant/wxPython-4.0.4/ext/wxWidgets/src/gtk/bitmap.cpp(581) in wxBitmap(): invalid image
您需要re-set缓冲区内的位置回零,以便wx.Image
从数据的开头读取,而不是结尾。
#Save into Buffer
buf = io.BytesIO()
plt.savefig(buf,format='png')
buf.seek(0)