|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | | - "signature": "sha256:fe1823edf76fa47c8093f8a895ac745863e32b256f461c740cb2a8b01c30b4f8" |
| 3 | + "signature": "sha256:1689807736a5c04f8d6174b694da4862c47c3b4b7291a591710eadd4a683b292" |
4 | 4 | }, |
5 | 5 | "nbformat": 3, |
6 | 6 | "nbformat_minor": 0, |
|
35 | 35 | "input": [ |
36 | 36 | "import math\n", |
37 | 37 | "\n", |
38 | | - "import pylab\n", |
39 | | - "import matplotlib\n", |
| 38 | + "import matplotlib.pyplot as plt\n", |
40 | 39 | "\n", |
41 | | - "\n", |
42 | | - "class AnnoteFinder:\n", |
43 | | - " \"\"\"\n", |
44 | | - " callback for matplotlib to display an annotation when points are clicked on. The\n", |
45 | | - " point which is closest to the click and within xtol and ytol is identified.\n", |
46 | | - " \n", |
47 | | - " Register this function like this:\n", |
48 | | - " \n", |
49 | | - " scatter(xdata, ydata)\n", |
50 | | - " af = AnnoteFinder(xdata, ydata, annotes)\n", |
51 | | - " connect('button_press_event', af)\n", |
52 | | - " \"\"\"\n", |
53 | | - "\n", |
54 | | - " def __init__(self, xdata, ydata, annotes, axis=None, xtol=None, ytol=None):\n", |
55 | | - " self.data = zip(xdata, ydata, annotes)\n", |
56 | | - " if xtol is None:\n", |
57 | | - " xtol = ((max(xdata) - min(xdata))/float(len(xdata)))/2\n", |
58 | | - " if ytol is None:\n", |
59 | | - " ytol = ((max(ydata) - min(ydata))/float(len(ydata)))/2\n", |
60 | | - " self.xtol = xtol\n", |
61 | | - " self.ytol = ytol\n", |
62 | | - " if axis is None:\n", |
63 | | - " self.axis = pylab.gca()\n", |
64 | | - " else:\n", |
65 | | - " self.axis= axis\n", |
66 | | - " self.drawnAnnotations = {}\n", |
67 | | - " self.links = []\n", |
| 40 | + "class AnnoteFinder(object):\n", |
| 41 | + " \"\"\"callback for matplotlib to display an annotation when points are\n", |
| 42 | + " clicked on. The point which is closest to the click and within\n", |
| 43 | + " xtol and ytol is identified.\n", |
68 | 44 | " \n", |
69 | | - " def distance(self, x1, x2, y1, y2):\n", |
70 | | - " \"\"\"\n", |
71 | | - " return the distance between two points\n", |
72 | | - " \"\"\"\n", |
73 | | - " return math.hypot(x1 - x2, y1 - y2)\n", |
74 | | - "\n", |
75 | | - " def __call__(self, event):\n", |
76 | | - " if event.inaxes:\n", |
77 | | - " clickX = event.xdata\n", |
78 | | - " clickY = event.ydata\n", |
79 | | - " if self.axis is None or self.axis==event.inaxes:\n", |
80 | | - " annotes = []\n", |
81 | | - " for x,y,a in self.data:\n", |
82 | | - " if clickX-self.xtol < x < clickX+self.xtol and clickY-self.ytol < y < clickY+self.ytol :\n", |
83 | | - " annotes.append((self.distance(x,clickX,y,clickY),x,y, a) )\n", |
84 | | - " if annotes:\n", |
85 | | - " annotes.sort()\n", |
86 | | - " distance, x, y, annote = annotes[0]\n", |
87 | | - " self.drawAnnote(event.inaxes, x, y, annote) \n", |
88 | | - " for l in self.links:\n", |
89 | | - " l.drawSpecificAnnote(annote)\n", |
| 45 | + " Register this function like this:\n", |
90 | 46 | " \n", |
91 | | - " def drawAnnote(self, axis, x, y, annote):\n", |
| 47 | + " scatter(xdata, ydata)\n", |
| 48 | + " af = AnnoteFinder(xdata, ydata, annotes)\n", |
| 49 | + " connect('button_press_event', af)\n", |
92 | 50 | " \"\"\"\n", |
93 | | - " Draw the annotation on the plot\n", |
94 | | - " \"\"\"\n", |
95 | | - " if (x,y) in self.drawnAnnotations:\n", |
96 | | - " markers = self.drawnAnnotations[(x,y)]\n", |
97 | | - " for m in markers:\n", |
98 | | - " m.set_visible(not m.get_visible())\n", |
99 | | - " self.axis.figure.canvas.draw()\n", |
100 | | - " else:\n", |
101 | | - " t = axis.text(x,y, \"(%3.2f, %3.2f) - %s\"%(x,y,annote), )\n", |
102 | | - " m = axis.scatter([x],[y], marker='d', c='r', zorder=100)\n", |
103 | | - " self.drawnAnnotations[(x,y)] =(t,m)\n", |
104 | | - " self.axis.figure.canvas.draw()\n", |
105 | | - "\n", |
106 | | - " def drawSpecificAnnote(self, annote):\n", |
107 | | - " annotesToDraw = [(x,y,a) for x,y,a in self.data if a==annote]\n", |
108 | | - " for x,y,a in annotesToDraw:\n", |
109 | | - " self.drawAnnote(self.axis, x, y, a)" |
| 51 | + "\n", |
| 52 | + " def __init__(self, xdata, ydata, annotes, ax=None, xtol=None, ytol=None):\n", |
| 53 | + " self.data = list(zip(xdata, ydata, annotes))\n", |
| 54 | + " if xtol is None:\n", |
| 55 | + " xtol = ((max(xdata) - min(xdata))/float(len(xdata)))/2\n", |
| 56 | + " if ytol is None:\n", |
| 57 | + " ytol = ((max(ydata) - min(ydata))/float(len(ydata)))/2\n", |
| 58 | + " self.xtol = xtol\n", |
| 59 | + " self.ytol = ytol\n", |
| 60 | + " if ax is None:\n", |
| 61 | + " self.ax = plt.gca()\n", |
| 62 | + " else:\n", |
| 63 | + " self.ax = ax\n", |
| 64 | + " self.drawnAnnotations = {}\n", |
| 65 | + " self.links = []\n", |
| 66 | + "\n", |
| 67 | + " def distance(self, x1, x2, y1, y2):\n", |
| 68 | + " \"\"\"\n", |
| 69 | + " return the distance between two points\n", |
| 70 | + " \"\"\"\n", |
| 71 | + " return(math.sqrt((x1 - x2)**2 + (y1 - y2)**2))\n", |
| 72 | + "\n", |
| 73 | + " def __call__(self, event):\n", |
| 74 | + "\n", |
| 75 | + " if event.inaxes:\n", |
| 76 | + "\n", |
| 77 | + " clickX = event.xdata\n", |
| 78 | + " clickY = event.ydata\n", |
| 79 | + " if (self.ax is None) or (self.ax is event.inaxes):\n", |
| 80 | + " annotes = []\n", |
| 81 | + " # print(event.xdata, event.ydata)\n", |
| 82 | + " for x, y, a in self.data:\n", |
| 83 | + " # print(x, y, a)\n", |
| 84 | + " if ((clickX-self.xtol < x < clickX+self.xtol) and\n", |
| 85 | + " (clickY-self.ytol < y < clickY+self.ytol)):\n", |
| 86 | + " annotes.append(\n", |
| 87 | + " (self.distance(x, clickX, y, clickY), x, y, a))\n", |
| 88 | + " if annotes:\n", |
| 89 | + " annotes.sort()\n", |
| 90 | + " distance, x, y, annote = annotes[0]\n", |
| 91 | + " self.drawAnnote(event.inaxes, x, y, annote)\n", |
| 92 | + " for l in self.links:\n", |
| 93 | + " l.drawSpecificAnnote(annote)\n", |
| 94 | + "\n", |
| 95 | + " def drawAnnote(self, ax, x, y, annote):\n", |
| 96 | + " \"\"\"\n", |
| 97 | + " Draw the annotation on the plot\n", |
| 98 | + " \"\"\"\n", |
| 99 | + " if (x, y) in self.drawnAnnotations:\n", |
| 100 | + " markers = self.drawnAnnotations[(x, y)]\n", |
| 101 | + " for m in markers:\n", |
| 102 | + " m.set_visible(not m.get_visible())\n", |
| 103 | + " self.ax.figure.canvas.draw_idle()\n", |
| 104 | + " else:\n", |
| 105 | + " t = ax.text(x, y, \" - %s\" % (annote),)\n", |
| 106 | + " m = ax.scatter([x], [y], marker='d', c='r', zorder=100)\n", |
| 107 | + " self.drawnAnnotations[(x, y)] = (t, m)\n", |
| 108 | + " self.ax.figure.canvas.draw_idle()\n", |
| 109 | + "\n", |
| 110 | + " def drawSpecificAnnote(self, annote):\n", |
| 111 | + " annotesToDraw = [(x, y, a) for x, y, a in self.data if a == annote]\n", |
| 112 | + " for x, y, a in annotesToDraw:\n", |
| 113 | + " self.drawAnnote(self.ax, x, y, a)" |
110 | 114 | ], |
111 | 115 | "language": "python", |
112 | 116 | "metadata": {}, |
113 | | - "outputs": [] |
| 117 | + "outputs": [], |
| 118 | + "prompt_number": 1 |
114 | 119 | }, |
115 | 120 | { |
116 | 121 | "cell_type": "markdown", |
|
127 | 132 | "y = range(10)\n", |
128 | 133 | "annotes = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", |
129 | 134 | "\n", |
130 | | - "scatter(x,y)\n", |
131 | | - "af = AnnoteFinder(x,y, annotes)\n", |
132 | | - "connect('button_press_event', af)" |
| 135 | + "fig, ax = plt.subplots()\n", |
| 136 | + "ax.scatter(x,y)\n", |
| 137 | + "af = AnnoteFinder(x,y, annotes, ax=ax)\n", |
| 138 | + "fig.canvas.mpl_connect('button_press_event', af)\n", |
| 139 | + "plt.show()" |
133 | 140 | ], |
134 | 141 | "language": "python", |
135 | 142 | "metadata": {}, |
|
0 commit comments