Python - 在 x y 图上以特定模式绘制数字
Python - draw numbers in specific pattern on x y graph
我想知道如何在 x y 图上绘制尽可能多的数字。它必须是交叉模式,像这样: 1 = (x0,y1); 2 = (x1,y0); 3 = (x0,y-1); 4 = (x-1,y0); 5 = (x0,y2); 6 = (x2,y0);等等...
我尝试过使用 matplotlib,但没有任何结果。
当前代码:
import matplotlib.pyplot as plt
x = [0, 1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0]
y = [1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0, 5]
plt.plot(x, y, "o", color="black")
plt.xlabel('x')
plt.ylabel('y')
for x, y in zip(x, y):
plt.text(x, y, str(x), color="red", fontsize=12)
plt.title('graph!')
plt.show()
IIUC,您想用列表中的索引来注释这些点。
使用enumerate
:
import matplotlib.pyplot as plt
x = [0, 1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0]
y = [1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0, 5]
plt.plot(x, y, "o", color="black")
plt.xlabel('x')
plt.ylabel('y')
for i, (x, y) in enumerate(zip(x, y)):
plt.text(x, y, i+1, color="red", fontsize=12)
plt.title('graph!')
plt.show()
输出:
我想知道如何在 x y 图上绘制尽可能多的数字。它必须是交叉模式,像这样: 1 = (x0,y1); 2 = (x1,y0); 3 = (x0,y-1); 4 = (x-1,y0); 5 = (x0,y2); 6 = (x2,y0);等等... 我尝试过使用 matplotlib,但没有任何结果。
当前代码:
import matplotlib.pyplot as plt
x = [0, 1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0]
y = [1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0, 5]
plt.plot(x, y, "o", color="black")
plt.xlabel('x')
plt.ylabel('y')
for x, y in zip(x, y):
plt.text(x, y, str(x), color="red", fontsize=12)
plt.title('graph!')
plt.show()
IIUC,您想用列表中的索引来注释这些点。
使用enumerate
:
import matplotlib.pyplot as plt
x = [0, 1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0]
y = [1, 0, -1, 0, 2, 0, -2, 0, 3, 0, -3, 0, 4, 0, -4, 0, 5]
plt.plot(x, y, "o", color="black")
plt.xlabel('x')
plt.ylabel('y')
for i, (x, y) in enumerate(zip(x, y)):
plt.text(x, y, i+1, color="red", fontsize=12)
plt.title('graph!')
plt.show()
输出: