问题描述
我尝试使用adjust_text
中的adjustText
函数来使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'))