1

I want to annotate points on a plot using the coordinates in the list I5. But running into an error. The expected output is attached.

import numpy as np
import matplotlib.pyplot as plt

I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

N=3   #len(inv_r)+1
X = np.arange(0,N,1)
Y = -X

for i in range(len(I5)):
    plt.annotate(I5[0][i])

x_sorted = np.sort(X)
y_sorted = np.sort(Y)
ax.set_xticks(x_sorted)
ax.set_yticks(y_sorted)
ax.set_xlim(x_sorted[0], x_sorted[-1])
ax.set_ylim(y_sorted[0], y_sorted[-1])
ax.grid()
ax.set_aspect('equal', 'box')
plt.show()

The error is

in <module>
    plt.annotate(I5[0][i])

  File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\_api\deprecation.py", line 335, in wrapper
    return func(*args, **kwargs)

TypeError: annotate() missing 1 required positional argument: 'xy'

The expected output is

enter image description here

1
  • I5 is the annotation string? I need the coordinates of each xy. Commented Jul 3, 2022 at 8:19

1 Answer 1

2
import numpy as np
import matplotlib.pyplot as plt

I5 = [[(0.5, -0.5), (0.5, -0.5)], [(0.5, -1.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -0.5)], [(1.5, -1.5), (1.5, -1.5)]]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

N=3   #len(inv_r)+1
X = np.arange(0,N,1)
Y = -X

for i in range(0,len(I5)):
    plt.annotate(I5[i][0],I5[i][1])

x_sorted = np.sort(X)
y_sorted = np.sort(Y)
ax.set_xticks(x_sorted)
ax.set_yticks(y_sorted)
ax.set_xlim(x_sorted[0], x_sorted[-1])
ax.set_ylim(y_sorted[0], y_sorted[-1])
ax.grid()
ax.set_aspect('equal', 'box')
plt.show()

In the I5 list the first tuple is the text you want to plot and the second tuple is the coordinates where you plot the text. I don't know if you want to add point but if you want to, you delete the first tuple to have only the coordinates tuple and then go

plt.plot(cord_tuple[0](x),cord_tuple[1](y))
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.