4

目前我有一个散点图,我可以放大、移动等
。我还想做的是能够在图表上选择一定数量的点,然后将选定的点存储在一个数组中。
我可以使用 matplotlib 中的任何特殊功能吗?
任何帮助将不胜感激

我的代码

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


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



def create_main_panel(self):

    self.panel = wx.Panel(self)
    self.dpi = 100
    self.fig = fg.Figure((5.0, 4.0), dpi=self.dpi)
    self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
    self.axes = self.fig.add_subplot(111)
    self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)
    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):
    self.axes.clear()
    x, y = [2,3,4,5]
    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()
4

1 回答 1

2

下面的代码提供了一个可能的解决方案。基本方法可以总结如下:

  • 一个附加了一个处理程序,pick_event它不断将选取的数据索引附加到一个列表中self._picked_indices
  • 每当用户按下键时,key_press_event处理程序都会清除列表。self._picked_indicesescape
  • 该方法MyFrame.picked_points返回当前选定点的坐标列表。None如果尚未选择任何点,则此方法返回(如果更方便,您可以修改它以在这种情况下返回一个空列表)。

这样,您可以通过单击它们来继续选择点。但是,如果您想重新开始,只需按下escape并重新开始采摘。

#! /usr/bin/env python
import os
import wx
import numpy as nump
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as fg
import matplotlib.backends.backend_wxagg as wxagg


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Title')
        self.create_main_panel()
        self.draw_figure()
        self._is_pick_started = False
        self._picked_indices = None

    def create_main_panel(self):
        self.panel = wx.Panel(self)
        self.dpi = 100
        self.fig = fg.Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
        self.axes = self.fig.add_subplot(111)
        self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)
        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)
        self.fig.canvas.mpl_connect('pick_event', self.on_pick)
        self.fig.canvas.mpl_connect('key_press_event', self.on_key)


    def draw_figure(self):
        self.axes.clear()
        self._x_data, self._y_data = [[2,3], [4,5]]
        self.axes.scatter(self._x_data, self._y_data, picker=5)
        self.canvas.draw()

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

    def picked_points(self):
        if self._picked_indices is None:
            return None
        else:
            return [ [self._x_data[i], self._y_data[i]]
                    for i in self._picked_indices ]

    def on_pick(self, event):
        if not self._is_pick_started:
            self._picked_indices = []
            self._is_pick_started = True

        for index in event.ind:
            if index not in self._picked_indices:
                self._picked_indices.append(index)
        print self.picked_points()

    def on_key(self, event):
        """If the user presses the Escape key then stop picking points and
        reset the list of picked points."""
        if 'escape' == event.key:
            self._is_pick_started = False
            self._picked_indices = None
        return


if __name__ == '__main__':
    app = wx.PySimpleApp()
    app.frame = MyFrame()
    app.frame.Show()
    app.MainLoop()
于 2013-02-20T00:10:59.727 回答