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 に答える