我正在用 Python 和 Tkinter 做一个项目。我可以绘制一组数据,并且还实现了一个在单击鼠标时在绘图上添加注释的功能,但现在我需要我添加的所有注释的列表。有什么办法可以做到这一点吗?
这是我添加注释的功能:
def onclick(self, event):
clicked = []
key = event.key
x = event.xdata
y = event.ydata
x_d = min(range(len(self.x_data)), key=lambda i: abs(self.x_data[i] - x))
local_coord = self.x_data[x_d - 6:x_d + 6]
x_1 = max(local_coord)
indx = np.where(self.x_data == x_1)[0][0]
y_1 = self.y_data[indx]
if key == "v":
self.ax.annotate("{0}nm".format(int(x_1)), size=25,
bbox=dict(boxstyle="round",fc="0.8"),
xy=(x_1, y_1), xycoords='data',
xytext=(x_1, y_1+50), textcoords='data',
arrowprops=dict(arrowstyle="-|>",
connectionstyle="bar,fraction=0",
))
self.canvas.draw()
你可以循环所有ax
孩子并检查孩子是否属于类型matplotlib.text.Annotation
:
for child in ax.get_children():
if isinstance(child, matplotlib.text.Annotation):
print("bingo") # and do something
或者,如果您想要一个列表:
annotations = [child for child in ax.get_children() if isinstance(child, matplotlib.text.Annotation)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)