Home > Blockchain >  Python - draw numbers in specific pattern on x y graph
Python - draw numbers in specific pattern on x y graph

Time:11-25

I wonder how can i draw as many as it's possible numbers on x y graph. It have to be cross pattern, something like this: 1 = (x0,y1); 2 = (x1,y0); 3 = (x0,y-1); 4 = (x-1,y0); 5 = (x0,y2); 6 = (x2,y0); and so on... I have tried with matplotlib, but without any results.

Current code:

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()

enter image description here

CodePudding user response:

IIUC, you want to annotate the points with their index in the list.

Use 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()

output:

enumerated points

  • Related