0

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

4

1 回答 1

1

Eli Bendersky在他的网站上发布了一些非常好的示例

这是他的一个例子,几乎被精简到最低限度:

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

在此处输入图像描述

于 2013-01-24T19:26:17.707 回答