How to include labels in matplotlib

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, at each point, a label indicating 1, 2, 3 and 4?

    
asked by anonymous 26.07.2017 / 03:48

1 answer

1

There is no ready-made functionality for this when generating the graph. You must use annotate () to mark every dot, or any text you want. As you know the points, just iterate over the values and print them on the chart:

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()
    
26.07.2017 / 12:54