1

Hello I want to add a label to individual points like in this :

Matplotlib scatter plot with different text at each data point

The code I want to insert labels into is as follows (https://mplsoccer.readthedocs.io/en/latest/gallery/plots/plot_scatter.html#sphx-glr-gallery-plots-plot-scatter-py):

from mplsoccer.pitch import Pitch
import matplotlib.pyplot as plt
plt.style.use('ggplot')

pitch = Pitch(figsize=(10, 8))
fig, ax = pitch.draw()
sc = pitch.scatter([70, 50, 20, 60, 90], [60, 50, 20, 10, 30],
                   c=['red', 'blue', 'green', 'yellow', 'orange'],
                   s=200, label='scatter', ax=ax)

n = ['21:46','28:52','19:05','82:25'] # Labels for each point

leg = ax.legend(borderpad=1, markerscale=0.5, labelspacing=1.5, loc='upper center', fontsize=15)

How can I get to each point and enter the labels?

Robxaa798
  • 171
  • 6

1 Answers1

1

I wrote the code on the assumption that the list of labels to be marked on the scatter plot is set to n. However, there is one less label for each data point.

from mplsoccer.pitch import Pitch
import matplotlib.pyplot as plt
plt.style.use('ggplot')

pitch = Pitch(figsize=(10, 8))
fig, ax = pitch.draw()
sc = pitch.scatter([70, 50, 20, 60, 90], [60, 50, 20, 10, 30],
                   c=['red', 'blue', 'green', 'yellow', 'orange'],
                   s=200, label='scatter', ax=ax)

n = ['21:46','28:52','19:05','82:25'] # Labels for each point
data = sc.get_offsets()

leg = ax.legend(borderpad=1, markerscale=0.5, labelspacing=1.5, loc='upper center', fontsize=15)

for idx,label in enumerate(n):
    ax.annotate(label, (data[idx][0]+2, data[idx][1]))

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32