我想使用串列中的坐標注釋繪圖上的點I5。但是遇到錯誤。附加了預期的輸出。
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()
錯誤是
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'
預期的輸出是

uj5u.com熱心網友回復:
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()
在I5串列中,第一個元組是您要繪制的文本,第二個元組是您繪制文本的坐標。我不知道你是否想添加點,但如果你想,你洗掉第一個元組只有坐標元組,然后去
plt.plot(cord_tuple[0](x),cord_tuple[1](y))
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497776.html
標籤:Python 麻木的 matplotlib
下一篇:根據名稱排列檔案
