KB 操作で戻すでよろしければ
Python
1import matplotlib.pyplot as plt
2plt.rcParams['font.family'] = 'MS Gothic'
3markers = list('ov^<>12348sp*hH+xD')
4colors = list('bgrcmyk')
5dots = []
6def motion(event):
7 global i, j, cnt
8 x, y = event.xdata, event.ydata
9 dot, = plt.plot([x], [y], color=colors[j], marker=markers[i], markersize=12)
10 dots.append(dot)
11 i, j = (i + 1) % len(markers), (j + 1) % len(colors)
12 cnt += 1
13 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right')
14 leg.legendHandles[0].set_visible(False)
15 plt.draw()
16
17def key_press(event):
18 global cnt
19 if dots and cnt > 1:
20 dots.pop().remove()
21 cnt -= 1
22 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right')
23 leg.legendHandles[0].set_visible(False)
24 plt.draw()
25
26fig, ax = plt.subplots()
27ax.set_xlim(0, 1)
28ax.set_ylim(0, 1)
29i = j = cnt = 0
30plt.connect('button_press_event', motion)
31plt.connect('key_press_event', key_press)
32plt.show()
右クリック版追加しておきます。
Python
1import matplotlib.pyplot as plt
2plt.rcParams['font.family'] = 'MS Gothic'
3markers = list('ov^<>12348sp*hH+xD')
4colors = list('bgrcmyk')
5dots = []
6def motion(event):
7 global i, j, cnt
8 if event.button == 3: # 右クリック
9 if dots and cnt > 0:
10 dots.pop().remove()
11 cnt -= 1
12 else:
13 x, y = event.xdata, event.ydata
14 dot, = plt.plot([x], [y], color=colors[j], marker=markers[i], markersize=12)
15 dots.append(dot)
16 i, j = (i + 1) % len(markers), (j + 1) % len(colors)
17 cnt += 1
18 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right')
19 if leg.legendHandles:
20 leg.legendHandles[0].set_visible(False)
21 plt.draw()
22
23plt.xlim(0, 1)
24plt.ylim(0, 1)
25i = j = cnt = 0
26plt.connect('button_press_event', motion)
27plt.show()