Python 带熊猫和牵牛星的表格气泡图
我有以下Python 带熊猫和牵牛星的表格气泡图,python,pandas,altair,Python,Pandas,Altair,我有以下df\u团队\u完整\u统计数据: Data columns (total 35 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Unnamed: 0 428 non-null int64 1 MatchID 428 non-nul
df\u团队\u完整\u统计数据
:
Data columns (total 35 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 428 non-null int64
1 MatchID 428 non-null int64
2 For Team 428 non-null object
3 Against Team 428 non-null object
4 Date 428 non-null object
5 GameWeek 428 non-null int64
6 Home 428 non-null object
7 Possession 428 non-null float64
8 Touches 428 non-null int64
9 Passes 428 non-null int64
10 Tackles 428 non-null int64
11 Clearances 428 non-null int64
12 Corners 428 non-null int64
13 Offsides 428 non-null int64
14 Fouls Committed 428 non-null int64
15 Yellow Cards 428 non-null int64
16 Goals 428 non-null int64
17 XG 428 non-null float64
18 Shots On Target 428 non-null int64
19 Total Shots 428 non-null int64
20 Goals Conceded 428 non-null int64
21 Shots Conceded 428 non-null int64
22 XGC 428 non-null float64
23 Shots In Box 428 non-null int64
24 Close Shots 428 non-null int64
25 Headers 428 non-null int64
26 Shots Centre 428 non-null int64
27 Shots Left 428 non-null int64
28 Shots Right 428 non-null int64
29 Shots In Box Conceded 428 non-null int64
30 Close Shots Conceded 428 non-null int64
31 Headers Conceded 428 non-null int64
32 Shots Centre Conceded 428 non-null int64
33 Shots Left Conceded 428 non-null int64
34 Shots Right Conceded 428 non-null int64
dtypes: float64(3), int64(28), object(4)
memory usage: 117.2+ KB
我正试图对其进行分组和排序,以实现此图表:
下面这个例子:,我们的想法是有一些目标“x”,比如“目标”,它将决定所有球队与所有其他球队比赛的泡沫大小
这就是我到目前为止所做的:
def team_match_ups(df_teams_full_stats, x, y):
target_x = x[0]
target_y = y[0]
df_temp = df_teams_full_stats.set_index("For Team")
df_temp.fillna(0.0, inplace=True)
df_temp[target_x] = df_temp.groupby(['For Team'])[target_x].mean()
X = df_temp[target_x].to_frame()
print ("x", X)
df_temp[target_y] = df_temp.groupby(['For Team'])[target_y].mean()
Y = df_temp[target_y].to_frame()
df = df_temp.reset_index()
#sorted_teams = sorted(df_teams_full_stats['For Team'].unique())
bubble_plot = alt.Chart(df).mark_circle().encode(
alt.Y(f'{target_x}:O', bin=True),
alt.X(f'{target_y}:O', bin=True),
alt.Size(f'{target_x}:Q',
scale=alt.Scale(range=[0, 1500])),
#alt.Color('Color', legend=None, scale=None),
tooltip = [alt.Tooltip(f'{target_x}:Q'),
alt.Tooltip(f'{target_y}:Q')],
)
return bubble_plot
但这远非理想:
问题
如何让团队名称命名x和y网格并设置气泡大小的目标?如果希望x和y轴成为团队名称,则应将x和y编码映射到包含团队名称的列。它可能看起来像这样(未经测试,因为您没有提供对数据的访问):
这很有效。两个注释:1)如何为给定目标分配“颜色”?2) 如何在图表顶部为x轴编写注释,以使其更简单?我的意思是如何将x轴的团队名称放在顶部?要分配颜色,请使用映射到要用颜色表示的列的
颜色编码。要在顶部确定轴的方向,请使用x=alt.x('For Team:N',axis=alt.axis(orient='top'))
alt.Chart(df_teams_full_stats).mark_circle().encode(
x='For Team:N',
y='Against Team:N',
size='Goals:Q'
)