2013-01-24 65 views
0

我想用matplotlib绘制散点图并将其嵌入到wxpython GUI中 我知道我必须使用matlibplot.use('wx.Agg'),但我不知道如何使用它并对其应用散点图。我找到的所有例子都是条形图,我不能将它应用到使用散点图 请帮助我wxpython在matlibplot中绘制散点图

回答

1

Eli Bendersky已发布一些very nice examples on his website

这里是他的一个例子,剥离下来到几乎最低限度:

import os 
import wx 
import numpy as np 
import matplotlib 
matplotlib.use('WXAgg') 
import matplotlib.figure as figure 
import matplotlib.backends.backend_wxagg as wxagg 


class MyFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, -1, 'Title') 
     self.create_menu() 
     self.create_main_panel() 
     self.draw_figure() 

    def create_menu(self): 
     self.menubar = wx.MenuBar() 

     menu_file = wx.Menu() 
     m_exit = menu_file.Append(-1, "&Quit\tCtrl-Q", "Quit") 
     self.Bind(wx.EVT_MENU, self.on_exit, m_exit) 
     self.menubar.Append(menu_file, "&File") 
     self.SetMenuBar(self.menubar) 

    def create_main_panel(self): 
     """ Creates the main panel with all the controls on it: 
      * mpl canvas 
      * mpl navigation toolbar 
      * Control panel for interaction 
     """ 
     self.panel = wx.Panel(self) 

     # Create the mpl Figure and FigCanvas objects. 
     # 5x4 inches, 100 dots-per-inch 
     # 
     self.dpi = 100 
     self.fig = figure.Figure((5.0, 4.0), dpi=self.dpi) 
     self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig) 
     self.axes = self.fig.add_subplot(111) 

     # Create the navigation toolbar, tied to the canvas 
     # 
     self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas) 

     # 
     # Layout with box sizers 
     # 
     self.vbox = wx.BoxSizer(wx.VERTICAL) 
     self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) 
     self.vbox.AddSpacer(25) 
     self.vbox.Add(self.toolbar, 0, wx.EXPAND) 

     self.panel.SetSizer(self.vbox) 
     self.vbox.Fit(self) 

    def draw_figure(self): 
     """ Redraws the figure 
     """ 
     # clear the axes and redraw the plot anew 
     # 
     self.axes.clear() 
     x, y = np.random.random((10, 2)).T 
     self.axes.scatter(x, y) 

     self.canvas.draw() 

    def on_exit(self, event): 
     self.Destroy() 


if __name__ == '__main__': 
    app = wx.PySimpleApp() 
    app.frame = MyFrame() 
    app.frame.Show() 
    app.MainLoop() 

enter image description here