如何使用线标记使matplotlib缩放和平移工具工作

时间:2015-05-22 16:40:17

标签: python numpy matplotlib wxpython

我开发了一个绘制光谱仪输入的程序。我尝试与之通信的设备被评定为发送不超过3个像素的不良信息(这部分是不可避免的)。我试图做的是让选择器选择3个可能的坏数据位,这样我就可以在固件中考虑它们,因为这三点不会改变。到目前为止,我的应用程序可以激活选择器(通过ctrl-shift-f12热键),我可以按预期选择和取消选择数据点。当我尝试使用NabigationToolbar2Wx()中提供的matplotlibs缩放或平移工具时出现问题。当我尝试使用其中任何一个时,我得到: ValueError: 'markevery' is iterable but not a valid form of numpy fancy indexing; markevery=[283, 286, 290, 292] 我正在使用python 2.7.9,matplotlib,以及wxPython演示文稿。

def on_pixel_picker(self, event):
    self.CaptureMouse()
    mouseevent = event.mouseevent
    line = self.axes.get_lines()[0]
    index = list(line.get_xdata()).index(int(mouseevent.xdata))
    if mouseevent.button is not 1:
        try:
            self.markers.remove(index)
        except ValueError:
            pass
        line.set_markevery(self.markers)
        self.figure.canvas.draw()
        self.ReleaseMouse()
        return
    if index in self.markers:
        return
    self.markers.append(index)
    self.markers.sort()
    line.set_marker('o')
    line.set_markevery(self.markers)
    self.figure.canvas.draw()
    self.ReleaseMouse()

这是追溯:

File "C:\Spectrometer\ApogeeSpectrovision.py", line 28, in <module>
  application.MainLoop()
File "C:\Python27\Lib\site-packages\wx-3.0-msw\wx\_core.py", line 8657, in MainLoop
  wx.PyApp.MainLoop(self)
File "C:\Python27\Lib\site-packages\wx-3.0-msw\wx\_core.py", line 7952, in MainLoop
  return _core_.PyApp_MainLoop(*args, **kwargs)
File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_wx.py", line 1016, in _onPaint
  self.draw(drawDC=drawDC)
File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_wxagg.py", line 46, in draw
  FigureCanvasAgg.draw(self)
File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_agg.py", line 469, in draw
  self.figure.draw(self.renderer)
File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
  draw(artist, renderer, *args, **kwargs)
File "C:\Python27\Lib\site-packages\matplotlib\figure.py", line 1085, in draw
  func(*args)
File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
  draw(artist, renderer, *args, **kwargs)
File "C:\Python27\Lib\site-packages\matplotlib\axes\_base.py", line 2110, in draw
  a.draw(renderer)
File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
  draw(artist, renderer, *args, **kwargs)
File "C:\Python27\Lib\site-packages\matplotlib\lines.py", line 737, in draw
  affine, self.axes.transAxes)
File "C:\Python27\Lib\site-packages\matplotlib\lines.py", line 181, in _mark_every_path
  'markevery=%s' % (markevery,))

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing; markevery=[283, 286, 290, 292]

关于我为什么会收到此错误以及如何解决此问题的任何想法?我的数据点非常接近,所以这个缩放和平移功能是必要的。

1 个答案:

答案 0 :(得分:0)

我认为那些有兴趣或可能有同样问题的人。我会先解决一下这个问题。向左平移绘图时,标记将在图形移动时保持在屏幕上的相同位置;这意味着标记会在您移动图形时跟踪图形。如果你将它向左移动到该线的点落下,则发生异常。如果将其移动到右侧,则标记将保持在该行的正确位置,但如果您将其移出屏幕,则会发生异常。工具栏中的缩放工具根本不起作用。一点击它就给了我一个例外。在line.py,730行左右的matplotib源代码深处,它相应地检查市场营销和图表;但是,如果市场营销不在可见指数中,则会发生例外情况。

    if markevery is not None:
        subsampled = _mark_every_path(new_markevery, tpath,
                                      affine, self.axes.transData)

我提出了修复:

    if markevery is not None:
        new_markevery = []
        xdata = self.get_xdata()
        ydata = self.get_ydata()
        for mark in markevery:
            point = (xdata[mark], ydata[mark])
            if point in tpath.vertices:
                new_markevery.append(np.where(tpath.vertices==point)[0][0])
        subsampled = _mark_every_path(new_markevery, tpath,
                                      affine, self.axes.transData)

确保该点可见,并获取tpath.vertices的新索引,而不是整个路径的索引。这是阻止它在平移时跟踪图形的关键。此修复程序还解决了缩放工具问题。