2

I want to label each data point on my pyplot.

I have two sets of data and I would like to label each data point with their value.

This is my code:

 import matplotlib.pyplot as plt

 x_position = [1,6,2,7,4,5]
 y_position = [8,4,7,7,2,4]

 plt.plot(x_position, y_position, 'rx')
 plt.show()

This plots a graph with a red marker for each point, however I need the data points to be displayed at each point.

Any help would be appreciated.

Thanks.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Marcus
  • 25
  • 1
  • 5

1 Answers1

2

Use plt.annotate:

import matplotlib.pyplot as plt

x_position = [1,6,2,7,4,5]
y_position = [8,4,7,7,2,4]

plt.plot(x_position, y_position, 'rx')

labels = ['text{}'.format(i) for i in range(len(x_positions))]
for label, x, y in zip(labels, x_position, y_position):
    plt.annotate(label, xy=(x, y), xytext=(2, 2),
    arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
Igor Yudin
  • 393
  • 4
  • 10