如何在散点图上标记这些点

How to label these points on the scatter plot

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_excel("path to the file")
fig, ax = plt.subplots()
fig.set_size_inches(7,3)
df = pd.DataFrame(data, columns = ['Player', 'Pos', 'Age'])
df.plot.scatter(x='Age',
                      y='Pos',
                      c='DarkBlue', xticks=([15,20,25,30,35,40]))
plt.show()

Got the plot but not able to label these points

如果您想标记每个点,您可以遍历绘制的每个坐标,在绘制点的位置使用 plt.text() 为其分配标签,如下所示:

from matplotlib import pyplot as plt

y_points = [i for i in range(0, 20)]
x_points = [(i*3) for i in y_points]

offset = 5

plt.figure()
plt.grid(True)
plt.scatter(x_points, y_points)

for i in range(0, len(x_points)):
    plt.text(x_points[i] - offset, y_points[i], f'{x_points[i]}')
    
plt.show()

在上面的示例中,它将给出以下内容:

偏移只是为了让标签更易读,使它们不在散点的正上方。

显然我们无法访问您的电子表格,但同样的基本概念也适用。

编辑

对于非数值,您可以简单地定义字符串作为坐标。可以这样做:

from matplotlib import pyplot as plt

y_strings = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
x_values = [i for i, string in enumerate(y_strings)]

# Plot coordinates:

plt.scatter(x_values, y_strings)

for i, string in enumerate(y_strings):
    plt.text(x_values[i], string, f'{x_values[i]}:{string}')

plt.grid(True)
plt.show()

这将提供以下输出: