I’m trying to associate lines with annotation with a label and I’m getting a “TypeError: cannot unpack non-iterable Annotation object” error. I’m rather new at python and I’m looking for a solution. The ultimate code would highlight the annotation and associated line when clicking either feature using mplcursors.
Below is a scaled down version of the code:
import numpy as np
import matplotlib.pyplot as plt
import mplcursors
def main():
fig, axes = plt.subplots()
lines = []
texts = []
for i in range(5):
llabel='line '+str(i)
line, = axes.plot((i + 1) * np.arange(10),label=llabel)
lines.append(line)
for i in range(5):
llabel='line '+str(i)
Last_y=(i + 1) * np.arange(10)
text, =axes.annotate(' '+llabel, xy=(9.00,Last_y[-1]), xytext=(3,0), color='black',
textcoords="offset points",
size=10, va="center" ,zorder=40,label=llabel )
texts.append(text)
plt.show()
if __name__ == "__main__":
main()