我试图创建一个大约1000个数据点的散点图,这样每个数据点都可以通过鼠标点击来选择,这将导致弹出一个上下文菜单,允许用户删除或更改数据点的颜色。我一直在学习matplotlib的教程,并学习可拖动矩形的练习,但是我遇到了困难。我正在使用matplotlib.patches.Circle类来表示每个数据点,但是我无法使用“contains”方法来正确处理“button_press_event”。主要是,似乎没有“画布”对象与每个圆对象关联。我在第16行得到以下错误:

AttributeError: 'NoneType' object has no attribute 'canvas'

代码如下:
#!/usr/bin/python -tt

import sys
import numpy
#import matplotlib.pyplot
from PyQt4 import QtGui, QtCore
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.patches import Circle

class SelectablePoint:
    def __init__(self, xy, label):
        self.point = Circle( (xy[0], xy[0]), .005 )
        self.label = label
        self.point.figure
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.onClick)

    def onClick(self, e):

        print e.xdata, e.ydata
        #print(dir(self.point))
        print self.point.center
        print self.point.contains(e)[0]
        #if self.point.contains(e)[0]:
        #    print self.label


class ScatterPlot(FigureCanvas):
    '''
    classdocs
    '''

    def __init__(self, parent=None):
        '''
        Constructor
        '''

        self.fig = Figure()
        FigureCanvas.__init__(self, self.fig)

        self.axes = self.fig.add_subplot(111)
        #x = numpy.arange(0.0, 3.0, 0.1)
        #y = numpy.cos(2*numpy.pi*x)
        x = [.5]
        y = [.5]

        #scatterplot = self.axes.scatter(x,y)

        for i in range(len(x)):
            c = Circle( (x[i], y[i]), .05 )
            self.axes.add_patch(c)
            #SelectablePoint( (x[i],y[i]), 'label for: ' + str(i), self.figure.canvas )
            SelectablePoint( (x[i],y[i]), 'label for: ' + str(i) )
            #self.axes.add_artist(c)

class MainContainer(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(900,600)
        self.setWindowTitle('Scatter Plot')

        sp = ScatterPlot(self)
        self.setCentralWidget(sp)

        self.center()

    def center(self):
        # Get the resolution of the screen
        screen = QtGui.QDesktopWidget().screenGeometry()

        # Get the size of widget
        size = self.geometry()
        self.move( (screen.width() - size.width())/2, (screen.height() - size.height())/2 )

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    b = MainContainer()
    b.show()
    sys.exit(app.exec_())

我不确定我的方法是否正确,或者我是否应该查看另一个绘图模块,但我已经查看了gnuplot和chaco,我觉得matplotlib更适合我的问题。还有其他的建议吗?非常感谢你。
**我的解决方案**
这是我到目前为止所做的工作。它只需在单击散点图中的点时,将每个数据点的标签输出到标准输出。
#!/usr/bin/python -tt

import sys
import numpy
from PyQt4 import QtGui, QtCore
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.patches import Circle

class SelectablePoint:
    def __init__(self, xy, label, fig):
        self.point = Circle( (xy[0], xy[1]), .25, figure=fig)
        self.label = label
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.onClick)

    def onClick(self, e):
        if self.point.contains(e)[0]:
            print self.label


class ScatterPlot(FigureCanvas):
    '''
    classdocs
    '''

    def __init__(self, parent=None):
        '''
        Constructor
        '''

        self.fig = Figure()
        FigureCanvas.__init__(self, self.fig)

        self.axes = self.fig.add_subplot(111)
        xlim = [0,7]
        ylim = [0,7]
        self.axes.set_xlim(xlim)
        self.axes.set_ylim(ylim)
        self.axes.set_aspect( 1 )

        x = [1, 1.2, 3, 4, 5, 6]
        y = [1, 1.2, 3, 4, 5, 6]
        labels = ['1', '2', '3', '4', '5', '6']

        for i in range(len(x)):
            sp = SelectablePoint( (x[i],y[i]), labels[i], self.fig)
            self.axes.add_artist(sp.point)

class MainContainer(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(900,600)
        self.setWindowTitle('Scatter Plot')

        sp = ScatterPlot(self)
        self.setCentralWidget(sp)

        self.center()

    def center(self):
        # Get the resolution of the screen
        screen = QtGui.QDesktopWidget().screenGeometry()

        # Get the size of widget
        size = self.geometry()
        self.move( (screen.width() - size.width())/2, (screen.height() - size.height())/2 )

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    b = MainContainer()
    b.show()
    sys.exit(app.exec_())

最佳答案

哦,我正在创建两个补丁实例。为每个数据点画一个圆圈。在将第一个实例传递给我的SelectablePoint类之后,一切正常。

关于python - 具有可点击和可选点的PyQt4散点图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5055972/

10-12 23:19