MatplotLib get all annotation by axes

2024/9/20 0:25:36

i'm doing a project with Python and Tkinter. I can plot an array of data and i also implemented a function to add annotation on plot when i click with the mouse, but now i need a list of all annotation that i added. Is there any way to have that? This is my function to add annotation:

def onclick(self, event):clicked = []key = event.keyx = event.xdatay = event.ydatax_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()
Answer

You could loop over all the ax children and check if the child is of type matplotlib.text.Annotation:

for child in ax.get_children():if isinstance(child, matplotlib.text.Annotation):print("bingo") # and do something

Or, if you want a list:

annotations = [child for child in ax.get_children() if isinstance(child, matplotlib.text.Annotation)]
https://en.xdnf.cn/q/72745.html

Related Q&A

Using Pandas to applymap with access to index/column?

Whats the most effective way to solve the following pandas problem? Heres a simplified example with some data in a data frame: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randin…

Multiple URL segment in Flask and other Python frameowrks

Im building an application in both Bottle and Flask to see which I am more comfortable with as Django is too much batteries included.I have read through the routing documentation of both, which is very…

installing python modules that require gcc on shared hosting with no gcc or root access

Im using Hostgator shared as a production environment and I had a problem installing some python modules, after using:pip install MySQL-pythonpip install pillowresults in:unable to execute gcc: Permiss…

Using libclang to parse in C++ in Python

After some research and a few questions, I ended up exploring libclang library in order to parse C++ source files in Python.Given a C++ source int fac(int n) {return (n>1) ? n∗fac(n−1) : 1; }for …

Python one class per module and packages

Im trying to structure my app in Python. Coming back from C#/Java background, I like the approach of one class per file. Id like my project tree to look like this:[Service][Database]DbClass1.pyDbClass2…

PyMySQL Access Denied using password (no) but using password

Headscratcher here for me.I am attempting to connect to a database on my local MySQL 8.0.11.0 install from Python.Heres the code Im using :conn = pymysql.connect(host=localhost, port=3306, user=root, p…

Trouble importing Python modules on Ninja IDE

I have been trying to import modules into Ninja IDE for python. These are modules that I have working on the terminal (numpy, scipy, scitools, matplotlib, and mpl_toolkits), but will not run correctly …

UTF-8 error with Python and gettext

I use UTF-8 in my editor, so all strings displayed here are UTF-8 in file.I have a python script like this:# -*- coding: utf-8 -*- ... parser = optparse.OptionParser(description=_(automates the dice ro…

Add build information in Jenkins using REST

Does anyone know how to add build information to an existing Jenkins build? What Im trying to do is replace the #1 build number with the actual full version number that the build represents. I can do …

Combining element-wise and matrix multiplication with multi-dimensional arrays in NumPy

I have two multidimensional NumPy arrays, A and B, with A.shape = (K, d, N) and B.shape = (K, N, d). I would like to perform an element-wise operation over axis 0 (K), with that operation being matrix …