Быстрый и простой пример в wxPython:
import wx
import math
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class Graph(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
self.Bind(wx.EVT_SIZE, self.OnSize)
def OnSize(self, event):
size = self.GetSize()
self.canvas.SetSize(size)
def Draw(self):
self.figure.clear()
self.subplot = self.figure.add_subplot(111)
x = [a*(math.pi/50) for a in range(100)]
y = [self.f(a) for a in x]
self.subplot.plot(x, y)
self.canvas.draw()
def DrawCosine(self, e):
self.f = math.cos
self.Draw()
def DrawSine(self, e):
self.f = math.sin
self.Draw()
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button1 = wx.Button(self.panel, label="Sine")
self.button2 = wx.Button(self.panel, label="Cosine")
self.graph = Graph(self.panel)
self.graph.DrawSine(None)
self.button1.Bind(wx.EVT_BUTTON, self.graph.DrawSine)
self.button2.Bind(wx.EVT_BUTTON, self.graph.DrawCosine)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer2 = wx.BoxSizer()
self.sizer.Add(self.graph, 1, wx.ALL | wx.EXPAND)
self.sizer2.Add(self.button1, 1, wx.ALL | wx.EXPAND)
self.sizer2.Add(self.button2, 1, wx.ALL | wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.ALL | wx.EXPAND)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Но, исходя из ваших требований, возможно, будет достаточно использовать (или немного изменить) pyplot
:
import wx
import math
from matplotlib import pyplot
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button1 = wx.Button(self.panel, label="Sine")
self.button2 = wx.Button(self.panel, label="Cosine")
self.button1.Bind(wx.EVT_BUTTON, self.DrawSine)
self.button2.Bind(wx.EVT_BUTTON, self.DrawCosine)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.button1)
self.sizer.Add(self.button2)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def Draw(self):
x = [a*(math.pi/50) for a in range(100)]
y = [self.f(a) for a in x]
pyplot.plot(x, y)
pyplot.grid(True, which='both', axis="both")
pyplot.show()
def DrawCosine(self, e):
self.f = math.cos
self.Draw()
def DrawSine(self, e):
self.f = math.sin
self.Draw()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()