matplotlib中注释对象的比较

时间:2017-09-20 17:13:49

标签: python python-3.x matplotlib

当该元素收到第二次点击时,我正在尝试删除散点图中元素的注释。我将新注释与当前出现在图上的注释列表进行比较。我得到的似乎是等价的值,但我的比较是返回false。我错过了什么?

以下是生成注释的onPick事件代码:

def displayval(self, ax):
    def onPick(event):

        thispoint = event.artist
        xval = thispoint.get_xdata()
        yval = thispoint.get_ydata()
        ind = event.ind

        if xval[ind].size > 1 or yval[ind].size > 1: return

        xy = (xval[ind][0], yval[ind][0])

        ann = ax.annotate('(%f , %f)' % xy, xy= xy)


        for elem in self.annotatedPoints:
            print(ann)
            print(elem)
            print (ann == elem)
            if ann  == elem:
                ann.remove()
                elem.remove()
                return

        self.annotatedPoints.append(ann)            

    self.figure.canvas.mpl_connect('pick_event', onPick)
    return onPick

这是运行时print语句的返回值,并且选择了两次相同的元素。

Annotation(3326,0.0262,'(3326.000000 , 0.026200)')
Annotation(3326,0.0262,'(3326.000000 , 0.026200)')
False

编辑: 完整的工作实例

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt

class eventerror():
    def __init__(self):

        self.annotatedPoints = []

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        ax = self.figure.add_subplot(1,1,1)

        xarray = [1,4,5,3,2,3]
        yarray = [7,8,2,4,3,9]
        val = self.displayval(ax)
        ax.plot(xarray, yarray, 'o', picker = 5)

        plt.show()

    def displayval(self, ax):
        def onPick(event):

            thispoint = event.artist
            xval = thispoint.get_xdata()
            yval = thispoint.get_ydata()
            ind = event.ind

            if xval[ind].size > 1 or yval[ind].size > 1: return

            xy = (xval[ind][0], yval[ind][0])

            ann = ax.annotate('(%f , %f)' % xy, xy= xy)
            plt.show()


            for elem in self.annotatedPoints:
                print(ann)
                print(elem)
                print (ann == elem)
                if ann  == elem:
                    ann.remove()
                    elem.remove()
                    return

            self.annotatedPoints.append(ann)            

        self.figure.canvas.mpl_connect('pick_event', onPick)
        return onPick

if __name__ == '__main__':
    err = eventerror()

解决方案:

    for elem in self.annotatedPoints:
        if ann.xy = elem.xy:
            ann.remove()
            elem.remove()
            self.annotatedPoints.remove(elem)
            return

1 个答案:

答案 0 :(得分:1)

Iterator<String[]> iter = stringArrayList.iterator(); while (iter.hasNext()) { String[] arr = iter.next(); for (int i = 0; i < arr.length; i++) { System.out.println("string" + i + " = " + arr[i]); } } 构造函数接受Annotationxytext以及arrowprops未透露的其他几个可选属性。您可能会看到__str__()。我建议你编写自己的等式谓词,提取感兴趣的特定项目,如x&amp; y,并测试那些。