Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a matplotlib figure inside a WxPython panel

How do I embed a matplotlib figure object inside a WxPython panel?

I googled around and saw complicated examples involving interactive graphics and other extra stuff. Can anybody help with a minimal example?

like image 454
Jesvin Jose Avatar asked May 24 '12 12:05

Jesvin Jose


People also ask

How do I show matplotlib in dashboard?

If you really need to use a matplotlib graph, you can export the generated image from matplotlib to base64 and use an html. Img component in your layout, filling out the src key with the string that is the base64 encoded image.

What is %Matplotlib inline?

You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

What is matplotlib Savefig?

savefig() As the name suggests savefig() method is used to save the figure created after plotting data. The figure created can be saved to our local machines by using this method.


2 Answers

This is a minimal example for a Panel with a matplotlib canvas:

from numpy import arange, sin, pi import matplotlib matplotlib.use('WXAgg')  from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wx import NavigationToolbar2Wx from matplotlib.figure import Figure  import wx  class CanvasPanel(wx.Panel):     def __init__(self, parent):         wx.Panel.__init__(self, parent)         self.figure = Figure()         self.axes = self.figure.add_subplot(111)         self.canvas = FigureCanvas(self, -1, self.figure)         self.sizer = wx.BoxSizer(wx.VERTICAL)         self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)         self.SetSizer(self.sizer)         self.Fit()      def draw(self):         t = arange(0.0, 3.0, 0.01)         s = sin(2 * pi * t)         self.axes.plot(t, s)   if __name__ == "__main__":     app = wx.PySimpleApp()     fr = wx.Frame(None, title='test')     panel = CanvasPanel(fr)     panel.draw()     fr.Show()     app.MainLoop() 

enter image description here

like image 128
joaquin Avatar answered Sep 19 '22 15:09

joaquin


Defining the frame size:

if __name__ == "__main__":
    app = wx.App()
    fr = wx.Frame(None, title='test', size=wx.Size(806, 450))
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

or defining the panel size:

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,size=wx.Size(806, 450))
...
like image 30
QCandia Avatar answered Sep 17 '22 15:09

QCandia