質問編集履歴

2

追加・修正内容についての項目を追加しました。

2021/09/23 12:43

投稿

yamatan
yamatan

スコア8

test CHANGED
File without changes
test CHANGED
@@ -161,3 +161,11 @@
161
161
 
162
162
 
163
163
  ```
164
+
165
+
166
+
167
+ # 追記・修正
168
+
169
+ 問題のプログラムを再現したコードを載せました。
170
+
171
+ 2次元座標上にランダムに配置した点を散布図として表示し、グラフ内の点をクリックすると、その点のエッジカラーが黄色に変化します。

1

問題のプログラムの再現コードを載せました。内容は、2次元座標にランダムに点を配置した散布図です。グラフ内の点をクリックするとクリックした点のエッジカラーが黄色に変化します。

2021/09/23 12:43

投稿

yamatan
yamatan

スコア8

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,137 @@
27
27
 
28
28
 
29
29
  ぜひ、アドバイスをお願いします。
30
+
31
+
32
+
33
+ # プログラム
34
+
35
+ ```Python
36
+
37
+ from tkinter.constants import X
38
+
39
+ import matplotlib.pyplot as plt
40
+
41
+ from matplotlib.collections import PathCollection
42
+
43
+ import numpy as np
44
+
45
+ import tkinter as tk
46
+
47
+ from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
48
+
49
+
50
+
51
+
52
+
53
+ def windowDestroy():
54
+
55
+ root.quit()
56
+
57
+ root.destroy()
58
+
59
+
60
+
61
+ def click(event):
62
+
63
+ global select_obj_ind
64
+
65
+ artist = event.artist
66
+
67
+ if isinstance(event.artist, PathCollection):
68
+
69
+ mouse_xy = np.array([event.mouseevent.xdata, event.mouseevent.ydata])
70
+
71
+ data_xy = np.array(artist.get_offsets().data)[event.ind]
72
+
73
+ object_ind = event.ind[np.argmax(np.square(data_xy-mouse_xy).sum(axis=1))] # 最も近いオブジェクトを選択
74
+
75
+ select_obj_ind = object_ind
76
+
77
+ updateCanvas()
78
+
79
+
80
+
81
+ def updateCanvas():
82
+
83
+ ax.cla()
84
+
85
+ ax.set_xlabel("x")
86
+
87
+ ax.set_ylabel("y")
88
+
89
+ edgecolors = ['none'] * N
90
+
91
+ if select_obj_ind != -1:
92
+
93
+ edgecolors[select_obj_ind] = 'yellow'
94
+
95
+ ax.scatter(x, y, edgecolor=edgecolors, picker=True, pickradius=2.5)
96
+
97
+ canvas.draw()
98
+
99
+
100
+
101
+ seed = 0
102
+
103
+ select_obj_ind = -1
104
+
105
+ print(f'initial seed:{seed}')
106
+
107
+ np.random.seed(seed=seed)
108
+
109
+ N = 100
110
+
111
+ x = np.random.randn(N)
112
+
113
+ y = np.random.randn(N)
114
+
115
+
116
+
117
+ fig = plt.figure()
118
+
119
+ ax = fig.add_subplot(111)
120
+
121
+ edgecolors = ['none'] * N
122
+
123
+ ax.set_xlabel("x")
124
+
125
+ ax.set_ylabel("y")
126
+
127
+ ax.scatter(x, y, edgecolor=edgecolors, picker=True, pickradius=2.5)
128
+
129
+
130
+
131
+ root = tk.Tk()
132
+
133
+ root.protocol('WM_DELETE_WINDOW', windowDestroy)
134
+
135
+
136
+
137
+ frame = tk.Frame(root)
138
+
139
+ frame.pack(side=tk.TOP, anchor=tk.CENTER, expand=True, fill=tk.BOTH)
140
+
141
+
142
+
143
+ canvas = FigureCanvasTkAgg(fig, master=frame)
144
+
145
+ canvas.draw()
146
+
147
+ canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
148
+
149
+ canvas.mpl_connect('pick_event', click)
150
+
151
+
152
+
153
+ toolbar = NavigationToolbar2Tk(canvas, frame)
154
+
155
+ toolbar.update()
156
+
157
+
158
+
159
+ root.mainloop()
160
+
161
+
162
+
163
+ ```