Search code examples
python-3.xmatplotlibwxpython

Displaying a matplotlib graph in a wxPython bitmap


I am trying to generate a graph from matplotlib, store it in a buffer, and then set it as a bitmap in a wxPython window.

The following code shows what I am trying to do. If I replace the buffer stored image with a PNG loaded from disk, the program executes as expected.

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

I get the following error message and the program stops:

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

Solution

  • You need to re-set the position within the buffer back to zero, so that wx.Image reads from the beginning of the data, not the end.

    #Save into Buffer
    buf = io.BytesIO()
    plt.savefig(buf,format='png')
    buf.seek(0)
    

    enter image description here