How to include Labels in matplotlib

Asked

Viewed 69 times

0

I have the following code:

import matplotlib.pyplot as plt

AP_X = [10,20,30,40]
AP_Y = [50,60,70,80]
plt.scatter(AP_X, AP_Y, color="green")
plt.ylim(0, 100)
plt.xlim(0, 100)

plt.show()

It displays a graph with the x,y positions of my 4 points.

How do I include, in each point, a label indicating 1, 2, 3 and 4?

1 answer

1


There is no feature ready for this when generating the chart. You need to use annotate( ) to mark each point, or any text, you want. As you know what the points are, just iterate on the values and print them on the graph:

import matplotlib.pyplot as plt

AP_X = [10,20,30,40]
AP_Y = [50,60,70,80]
plt.scatter(AP_X, AP_Y, color="green")
plt.ylim(0, 100)
plt.xlim(0, 100)

### IMPRIMIDO AS COORDENADAS ###
for i in range(len(AP_X)):
    label = '(' + str(AP_X[i]) + ', ' + str(AP_Y[i]) + ')'
    plt.annotate(label, xy=(AP_X[i], AP_Y[i]), xytext=(AP_X[i]+2, AP_Y[i]+2))

plt.show()
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.