0

Currently I'm developing a GUI in WxPython.

I have created a lineplot using matplotib and a gridview of the underlying data. However, as I run the program, the grid and the plot are loaded into separate windows. I'm looking for examples or tutorials on how to integrate different items into a single window. Does anyone know any good example or tutorials on this? I cannot find a clear example.

Thanks!

Arjan Groen
  • 604
  • 8
  • 16

2 Answers2

0

Recently I used matplotlib with wx to plot some histograms, this solution maybe can help you to solve your problem. The code is inherited from a panel, and you need to add this component like a panel. To set the data use the function SetData.

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class HistogramPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure(figsize=(-1, -1))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axis = self.figure.add_subplot(111)

        self.rootSizer = wx.BoxSizer(wx.VERTICAL)
        self.rootSizer.Add(self.canvas, 1, wx.EXPAND | wx.GROW)
        self.SetSizer(self.rootSizer)

        self.FitInside()
        self.Layout()

    def SetData(self, values, color="black", linewidth=1):
        self.axis.clear()
        self.axis.plot(values, color=color, linewidth=linewidth)
        self.axis.set_xlim([0, 256])
        self.Layout()
  • just as a side note, there is `set_ydata` method for faster update of an existing plot. check http://stackoverflow.com/a/4098938/566035. but this will not take care of data range change, autoscaling etc. so, not a general solution like your code snippet. – otterb Jun 04 '16 at 22:23
0

Here is an example code Embedding a matplotlib figure inside a WxPython panel

Basically, to hold a matplotlib figure you can use FigureCanvasWxAgg which can be imported like:

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg

and, you will do the normal plotting on Figure instance of MPL.

from matplotlib.figure import Figure

and put this Figure on FigureCanvasWxAgg which can be placed on a panel together with your grid using sizer.

Community
  • 1
  • 1
otterb
  • 2,660
  • 2
  • 29
  • 48