Last active
September 14, 2020 16:15
-
-
Save dmgl/681c3dcc3199b67134a838a9a27803fd to your computer and use it in GitHub Desktop.
Visualization of our kicker game scores (matplotlib)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import json | |
import matplotlib.pyplot as plt | |
j_file = open('/home/dmgl/Downloads/Telegram Desktop/ChatExport_2020-09-04/result.json', 'r') | |
j = json.load(j_file) | |
s_type = {'type': 'hashtag', 'text': '#scores'} | |
scores = [] # list of lists with score pairs [[5, 4], [5, 5], [5, 6], [7, 7], [8, 8], [9, 8], [9, 9], [10, 9], ...] | |
dates = [] # list of strings dates ['2017-09-04T20:25:41', '2017-09-05T18:18:35', '2017-09-05T20:42:58', ...] | |
for message in j['messages']: | |
for text in message['text']: | |
if text == s_type: | |
date = message['date'] | |
# print(message) | |
try: | |
score = re.findall(r"[-+]?\d*\.\d+|\d+", message['text'][1]) | |
# print(message['text']) | |
# print(date, score) | |
except: | |
pass | |
scores.append(score) | |
dates.append(date) | |
team1 = "GALOCHKIN, KLIMOV" | |
team2 = "OVCHAROV, STANOTIN" | |
times = [ d for d in range(len(dates)) ] | |
scores = [ [int(x[0]), int(x[1])] for x in scores ] | |
def variant1(): | |
# VARIANT 1 - SHOW SUMMARY PROGRESS IN SCORES | |
x1 = times | |
x2 = times | |
y1 = [ x[0] for x in scores ] | |
y2 = [ x[1] for x in scores ] | |
with plt.xkcd(): | |
fig, axs = plt.subplots(figsize=(30, 10)) | |
axs.plot(x1, y1, label=team1) | |
axs.plot(x2, y2, label=team2) | |
axs.set(xlabel='GAMES', ylabel='SCORES') | |
fig.suptitle('KICKER') | |
# fig.tight_layout() | |
fig.savefig("KICKER_SCORES_GRAPH_SUMMARY.PNG", bbox_inches='tight') | |
plt.legend() | |
plt.show() | |
def variant2(): | |
# VARIANT 2 - SHOW DIFF BETWEEN TEAMS SCORES | |
x1 = times | |
x2 = times | |
y1 = [] | |
y2 = [] | |
for s in scores: | |
y1.append( (s[0]-s[1])/2 ) | |
y2.append( (s[1]-s[0])/2 ) | |
def get_triumph_times(scores): | |
max1,max2 = (0, 0) | |
for pair in scores: | |
if pair[0] > pair[1]: | |
new_max1 = pair[0] - pair[1] | |
new_ts1 = scores.index(pair) | |
if new_max1 > max1: | |
max1 = new_max1 | |
ts1 = new_ts1 | |
else: | |
new_max2 = pair[1] - pair[0] | |
new_ts2 = scores.index(pair) | |
if new_max2 > max2: | |
max2 = new_max2 | |
ts2 = new_ts2 | |
dt1 = dates[ts1] | |
dt2 = dates[ts2] | |
return { team1: (ts1, dt1, max1), | |
team2: (ts2, dt2, max2) } | |
triumph_times = get_triumph_times(scores) | |
with plt.xkcd(): | |
fig, axs = plt.subplots(figsize=(30, 10)) | |
axs.plot(x1, y1, label=team1) | |
axs.plot(x2, y2, label=team2) | |
axs.set(xlabel='GAMES', ylabel='SCORES') | |
fig.suptitle('KICKER') | |
# fig.tight_layout() | |
half = times[ -int(len(times)/2) ] | |
plt.annotate( | |
'THE BEGINNING ' + dates[0], | |
xy=(times[0], 0), | |
xytext=(half, -1.5), | |
arrowprops=dict(arrowstyle='->'), | |
bbox=dict(boxstyle="round", fc="0.9"), | |
ha='center', va='center' | |
) | |
plt.annotate( | |
'THE END ' + dates[-1], | |
xy=(times[-1], 0), | |
arrowprops=dict(arrowstyle='->'), | |
xytext=(half, -0.5), | |
bbox=dict(boxstyle="round", fc="0.9"), | |
ha='center', va='center' | |
) | |
plt.annotate( | |
'TRIUMPH ' + team1 + " " + triumph_times[team1][1], | |
xy=( times[ triumph_times[team1][0] ], triumph_times[team1][2] /2 ), | |
arrowprops=dict(arrowstyle='wedge',), | |
xytext=(half, 0.5), | |
bbox=dict(boxstyle="round", fc="0.9"), | |
ha='center', va='center', | |
size=20 | |
) | |
plt.annotate( | |
'TRIUMPH ' + team2 + " " + triumph_times[team2][1], | |
xy=( times[ triumph_times[team2][0] ], triumph_times[team2][2] /2), | |
arrowprops=dict(arrowstyle='wedge'), | |
xytext=(half, 1.5), | |
bbox=dict(boxstyle="round", fc="0.9"), | |
ha='center', va='center', | |
size=20 | |
) | |
fig.savefig("KICKER_SCORES_GRAPH_DIFF.PNG", bbox_inches='tight') | |
plt.legend() | |
plt.show() | |
# variant1() | |
# variant2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import json | |
j_file = open('/home/dmgl/Downloads/Telegram Desktop/ChatExport_2020-09-04/result.json', 'r') | |
j = json.load(j_file) | |
s_type = {'type': 'hashtag', 'text': '#scores'} | |
scores = [] # list of lists with score pairs [[5, 4], [5, 5], [5, 6], [7, 7], [8, 8], [9, 8], [9, 9], [10, 9], ...] | |
dates = [] # list of strings dates ['2017-09-04T20:25:41', '2017-09-05T18:18:35', '2017-09-05T20:42:58', ...] | |
for message in j['messages']: | |
for text in message['text']: | |
if text == s_type: | |
date = message['date'] | |
# print(message) | |
try: | |
score = re.findall(r"[-+]?\d*\.\d+|\d+", message['text'][1]) | |
# print(message['text']) | |
# print(date, score) | |
except: | |
pass | |
scores.append(score) | |
dates.append(date) | |
team1 = "GALOCHKIN, KLIMOV" | |
team2 = "OVCHAROV, STANOTIN" | |
times = [ d for d in range(len(dates)) ] | |
scores = [ [int(x[0]), int(x[1])] for x in scores ] | |
team1_scores = [ x[0] for x in scores ] | |
team2_scores = [ x[1] for x in scores ] | |
team1_scores_diff = [] | |
team2_scores_diff = [] | |
for s in scores: | |
team1_scores_diff.append( (s[0]-s[1])/2 ) | |
team2_scores_diff.append( (s[1]-s[0])/2 ) | |
import pandas as pd | |
import plotly.express as px | |
import plotly.graph_objects as go | |
# COUNT DATES HEATMAP | |
fig_heat = px.density_heatmap(dates, labels=dict(x='GAMES',y='DATES'), x=times, y=dates) | |
fig_heat.update_layout(title_text="COUNT DATES HEATMAP", title_font_size=20) | |
fig_heat.show() | |
# SUMMARY PROGRESS IN SCORES | |
x1 = dates | |
x2 = dates | |
y1 = team1_scores | |
y2 = team2_scores | |
df1 = pd.DataFrame.from_records([x1,y1]) | |
df2 = pd.DataFrame.from_records([x2,y2]) | |
fig_summary = go.Figure() | |
fig_summary.add_trace(go.Scatter(x=df1.values[0], y=df1.values[1], | |
name=team1, line_shape='linear', | |
showlegend=True,)) | |
fig_summary.add_trace(go.Scatter(x=df2.values[0], y=df2.values[1], | |
name=team2, line_shape='linear', | |
showlegend=True,)) | |
fig_summary.update_layout(title_text="SUMMARY PROGRESS IN SCORES", title_font_size=20) | |
fig_summary.show() | |
# DIFF BETWEEN TEAMS SCORES | |
x1 = dates | |
x2 = dates | |
y1 = team1_scores_diff | |
y2 = team2_scores_diff | |
df1 = pd.DataFrame.from_records([x1,y1]) | |
df2 = pd.DataFrame.from_records([x2,y2]) | |
fig_diff = go.Figure() | |
fig_diff.add_trace(go.Scatter(x=df1.values[0], y=df1.values[1], | |
name=team1, line_shape='spline', | |
showlegend=True,)) | |
fig_diff.add_trace(go.Scatter(x=df2.values[0], y=df2.values[1], | |
name=team2, line_shape='spline', | |
showlegend=True,)) | |
fig_diff.update_layout(title_text="DIFF BETWEEN TEAMS SCORES", title_font_size=20) | |
fig_diff.show() | |
# ANOTHER SUMMARY PROGRESS IN SCORES | |
x1 = times | |
x2 = times | |
y1 = team1_scores | |
y2 = team2_scores | |
df1 = pd.DataFrame.from_records([x1,y1]) | |
df2 = pd.DataFrame.from_records([x2,y2]) | |
fig_teams = go.Figure() | |
fig_teams.add_trace(go.Scatter(x=df1.values[0], y=df1.values[1], | |
name=team1, line_shape='hvh', | |
mode='lines+markers', | |
showlegend=True, | |
marker=dict(size=[x/5 for x in y1],color='darkgrey'), | |
)) | |
fig_teams.add_trace(go.Scatter(x=df2.values[0], y=df2.values[1], | |
name=team2, line_shape='hvh', | |
mode='lines+markers', | |
showlegend=True, | |
marker=dict(size=[x/5 for x in y2],color='lightgrey'), | |
)) | |
fig_teams.update_layout(barmode='overlay') | |
fig_teams.update_traces(opacity=0.9) | |
fig_teams.update_layout(title_text="ANOTHER SUMMARY PROGRESS IN SCORES", title_font_size=20) | |
fig_teams.show() | |
# BARS BY SCORES | |
fig_bars = go.Figure() | |
fig_bars.add_trace(go.Histogram(go.Histogram(x=team1_scores),name=team1,showlegend=True)) | |
fig_bars.add_trace(go.Histogram(go.Histogram(x=team2_scores),name=team2,showlegend=True)) | |
fig_bars.update_layout(title_text="SCORES BARS", title_font_size=20) | |
fig_bars.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment