我正试图自定义在[此处](Possible to make labels appear when hovering over a point in matplotlib?)找到的代码,以便为绘图中的点创建标签。
在使用plot选项的代码段中,他们使用以下行:
line, = plt.plot(x,y, marker="o")
之后,在其余代码中继续使用line变量(例如):
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = line.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
在我的代码中,我以以下方式创建点图,而不是创建线(我不使用散点图,因为我需要通过其位置来创建点-为此调整标记的颜色和形状,并我为此感到困惑与分散):
def create_plot(self):
for s in self.soldiers:
if s.company_number == 1:
if type(s) == Soldier:
self.canvas.ax.plot(s.x, s.y, marker='o', markersize=5, color="blue", picker=5, label=s.__str__())
else:
self.canvas.ax.plot(s.x, s.y, marker='*', markersize=5, color="blue", picker=5, label=s.__str__())
elif s.company_number == 2:
if type(s) == Soldier:
self.canvas.ax.plot(s.x, s.y, marker='o', markersize=5, color="red", picker=5, label=s.__str__())
else:
self.canvas.ax.plot(s.x, s.y, marker='*', markersize=5, color="red", picker=5, label=s.__str__())
elif s.company_number == 3:
if type(s) == Soldier:
self.canvas.ax.plot(s.x, s.y, marker='o', markersize=5, color="green", picker=5, label=s.__str__())
else:
self.canvas.ax.plot(s.x, s.y, marker='*', markersize=5, color="green", picker=5, label=s.__str__())
else:
continue
为了自定义create_plot函数的行变量,我应该在代码中进行哪些更改?