python – 从图形matplotlib中选择点

目前我有一个散点图,我可以放大,移动等.

我还想做的是能够在图表上选择一定数量的点,然后将选定的点存储在一个数组中.

我可以使用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()

最佳答案 以下代码提供了可能的解决方案.基本方法可概括如下:

> one附加了pick_event的处理程序,该处理程序将拾取的数据索引附加到列表self._picked_indices.
>只要用户按下转义键,key_press_event处理程序就会清除self._picked_indices列表.
>方法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()
点赞