如何调整 Matplotlib 散点图中的文本以使散点不重叠?

How to adjust text in Matplotlib scatter plot so scatter points don't overlap?

我尝试使用 adjustText 中的 adjust_text 函数来使 matplotlib 中的散点标签不重叠。

#Adding the names
for i, txt in enumerate(bigdf['Player']):
    if bigdf['Goals'][i] >= 5 or bigdf["Assists"][i] >= 3:
        ax.annotate(txt, (bigdf['Goals'][i]+0.15, bigdf["Assists"][i]))
        adjust_text(ax.annotate, x=bigdf['Goals'], y=bigdf["Assists"])
    else:
        None

我正在使用位于数据框 (bigdf) 中的数据,我希望玩家名称显示在图表上的散点旁边。但是,当我绘制它们时,一些名称重叠并使其不可读。我尝试了以下代码来尝试调整文本,使它们不重叠但无济于事。

这就是现在的样子:

有什么建议吗?

adjust_text()的要点是通过以列表形式给你要注释的文本来实现的:第一张图没有修饰,第二张图有指向分散值的箭头。注意:部分散点标记因未知原因丢失。

import pandas as pd

df = pd.read_csv('./Data/PremierLeague_1920.csv', encoding='utf-8')
df.head()
|    |   RANK | PLAYER                    | TEAM            |   GP |   GS |   MIN |   G |   ASST |   SHOTS |   SOG |
|---:|-------:|:--------------------------|:----------------|-----:|-----:|------:|----:|-------:|--------:|------:|
|  0 |      1 | Jamie Vardy               | Leicester City  |   35 |   34 |  3034 |  23 |      5 |      71 |    43 |
|  1 |      2 | Daniel William John Ings  | Southampton     |   38 |   32 |  2812 |  22 |      2 |      66 |    38 |
|  2 |      3 | Pierre-Emerick Aubameyang | Arsenal         |   36 |   35 |  3138 |  22 |      3 |      70 |    42 |
|  3 |      4 | Raheem Shaquille Sterling | Manchester City |   33 |   30 |  2660 |  20 |      1 |      68 |    38 |
|  4 |      5 | Mohamed Salah Ghaly       | Liverpool       |   34 |   33 |  2884 |  19 |     10 |      95 |    59 |

# 2team pick up
df1 = df[(df['TEAM'] == 'Leicester City') | (df['TEAM'] == 'Liverpool')]

import matplotlib.pyplot as plt
from adjustText import adjust_text

fig = plt.figure(figsize=(6,6),dpi=144)
ax = fig.add_subplot(111)

players = []
team_name = ['Leicester City','Liverpool']
for index, row in df1.iterrows():
    player_name = row[1]
    team = row[2]
    goal = row[6]
    assist = row[7]
    if team == team_name[0]:
        color = 'b'
    else:
        color = 'r'
    ax.scatter(goal, assist, c=color, s=25, alpha=0.8, edgecolors='none')
    if goal >=5 or assist >=3:
        players.append(ax.annotate(player_name, xy=(goal + 1, assist + 1), size=8))

adjust_text(players)
ax.legend(loc='best', labels=team_name)
ax.grid(False)

plt.show()

adjust_text(players, arrowprops=dict(arrowstyle='->', color='red'))