-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_plot.py
executable file
·104 lines (87 loc) · 3.29 KB
/
local_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ========== (c) JP Hwang 2020-02-29 ==========
import logging
# ===== START LOGGER =====
logger = logging.getLogger(__name__)
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
sh.setFormatter(formatter)
root_logger.addHandler(sh)
import pandas as pd
import numpy as np
desired_width = 320
pd.set_option('display.max_columns', 20)
pd.set_option('display.width', desired_width)
# ========== LOAD DATA ==========
all_teams_df = pd.read_csv('srcdata/shot_dist_compiled_data_2019_20.csv')
# ========== FIRST CHART VERSION ==========
import plotly.express as px
fig = px.scatter(all_teams_df[all_teams_df.group == 'NOP'], x='min_mid', y='player', size='shots_freq', color='pl_pps')
fig.show()
# ========== REFINED CHART VERSION ==========
def clean_chart_format(fig):
import plotly.graph_objects as go
fig.update_layout(
paper_bgcolor="white",
plot_bgcolor="white",
annotations=[
go.layout.Annotation(
x=0.9,
y=1.02,
showarrow=False,
text="Twitter: @_jphwang",
xref="paper",
yref="paper",
textangle=0
),
],
font=dict(
family="Arial, Tahoma, Helvetica",
size=10,
color="#404040"
),
margin=dict(
t=20
)
)
fig.update_traces(marker=dict(line=dict(width=1, color='Navy')),
selector=dict(mode='markers'))
fig.update_coloraxes(
colorbar=dict(
thicknessmode="pixels", thickness=15,
outlinewidth=1,
outlinecolor='#909090',
lenmode="pixels", len=300,
yanchor="top",
y=1,
))
fig.update_yaxes(showgrid=True, gridwidth=1, tickson='boundaries', gridcolor='LightGray', fixedrange=True)
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='LightGray', fixedrange=True)
return True
def make_shot_dist_chart(input_df, color_continuous_scale=None, size_col='shots_count', col_col='pl_acc', range_color=None):
max_bubble_size = 15
if color_continuous_scale is None:
color_continuous_scale = px.colors.diverging.RdYlBu_r
if range_color is None:
range_color = [min(input_df[col_col]), max(input_df[col_col])]
fig = px.scatter(
input_df, x='min_mid', y='player', size=size_col,
color=col_col,
color_continuous_scale=color_continuous_scale,
range_color=range_color,
range_x=[0, 49],
range_y=[-1, len(input_df.player.unique())],
hover_name='player', hover_data=['min_start', 'min_end', 'shots_count', 'shots_made', 'shots_freq', 'shots_acc', ],
render_mode='svg'
)
fig.update_coloraxes(colorbar=dict(title='Points per<BR>100 shots'))
fig.update_traces(marker=dict(sizeref=2. * 30 / (max_bubble_size ** 2)))
fig.update_yaxes(title="Player")
fig.update_xaxes(title='Minute', tickvals=list(range(0, 54, 6)))
return fig
fig = make_shot_dist_chart(
all_teams_df[all_teams_df.group == 'SAS'], col_col='pl_pps', range_color=[90, 120], size_col='shots_freq')
clean_chart_format(fig)
fig.update_layout(height=500, width=1250)
fig.show()