【问题标题】:selecting points from a graph matplotlib从图 matplotlib 中选择点
【发布时间】:2023-03-20 01:38:02
【问题描述】:

目前我有一个散点图,可以放大、移动等。
我还想做的是能够在图形上选择一定数量的点,然后将选定的点存储在一个数组中。
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()

【问题讨论】:

  • 我认为这对你不起作用:stackoverflow.com/questions/7969352/…
  • 我只能选择单个点
  • 你能一直迭代pick_events直到你点击图表的一个特殊区域(例如,离开轴)?
  • 或许看看缩放矩形是如何实现的

标签: python matplotlib wxpython


【解决方案1】:

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

  • pick_event 附加一个处理程序,该处理程序不断将选取的数据索引附加到列表self._picked_indices
  • 只要用户按下escape 键,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()

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 2013-09-13
    • 1970-01-01
    • 2018-08-12
    • 2023-04-03
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多