我正在用 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
child 并检查 child 是否属于 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)]
关于python - MatplotLib 通过轴获取所有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841048/