我想使用matplotlib绘制散点图并将其嵌入到wxpython GUI中 我知道我必须使用matlibplot.use('wx.Agg'),但我不确定如何使用它并应用散点图。我发现的所有示例都是条形图,我不能将它应用于使用散点图 请帮帮我
答案 0 :(得分: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()