Spaces:
Runtime error
Runtime error
File size: 3,714 Bytes
b400a06 dc79570 b400a06 6fc69ec b400a06 6fc69ec b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 dc79570 b400a06 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import json
from collections import Counter
import numpy as np
import pandas as pd
import altair as alt
import operator
import warnings
warnings.filterwarnings('ignore')
# loading the events data
events={}
nations = ['Italy','England','Germany','France','Spain','European_Championship','World_Cup']
for nation in nations:
with open('./events_%s.json' %nation) as json_data:
events[nation] = json.load(json_data)
# loading the match data
matches={}
nations = ['Italy','England','Germany','France','Spain','European_Championship','World_Cup']
for nation in nations:
with open('./matches_%s.json' %nation) as json_data:
matches[nation] = json.load(json_data)
# loading the players data
players={}
with open('players.json') as json_data:
players = json.load(json_data)
# loading the competitions data
competitions={}
with open('competitions.json') as json_data:
competitions = json.load(json_data)
ev_all_nations = []
for nation in nations:
for i in range(len(events[nation])):
ev_all_nations.append(events[nation][i]['eventName'])
count = Counter(ev_all_nations)
total = len(ev_all_nations)
counter = {event: int((count / total) * 100) for event, count in count.items()}
sorted_counter = sorted(counter.items(), key=operator.itemgetter(1), reverse=False)
data = pd.DataFrame(sorted_counter, columns=['Event', 'Percentage'])
brush = alt.selection_interval(encodings=['y'])
max_value = data['Percentage'].max()
tick_values = list(range(0, int(max_value) + 10, 10))
bars = alt.Chart(data).mark_bar().encode(
y=alt.Y('Event:N', title=None, sort='-x'),
x=alt.X('Percentage:Q', title='events(%)', axis=alt.Axis(values=tick_values)),
color=alt.condition(brush, alt.Color('Event:N', legend=None), alt.value('lightgray'))
).add_selection(
brush
)
average_rule = alt.Chart(data).mark_rule(color='firebrick', strokeWidth=2).encode(
x='mean(Percentage):Q'
).transform_filter(
brush
)
average_text = alt.Chart(data).mark_text(
dx=5, dy=-5, color='firebrick', align='left', fontWeight='bold'
).encode(
x=alt.X('mean(Percentage):Q', aggregate='mean'),
text=alt.Text('mean(Percentage):Q', aggregate='mean', format='.1f')
).transform_filter(
brush
)
chart1 = alt.layer(bars, average_rule, average_text).properties(
width=600,
height=500,
title='Events Distribution'
)
# Display the combined chart
chart1
match_ev_count = {}
for nation in nations:
for ev in events[nation]:
if ev['matchId'] not in match_ev_count:
match_ev_count[ev['matchId']] = 1
else:
match_ev_count[ev['matchId']] += 1
data = pd.DataFrame({
'Event Count': list(match_ev_count.values())
})
event_count_values = list(match_ev_count.values())
min_value = min(event_count_values)
max_value = max(event_count_values)
ticks = list(range((min_value // 200) * 200, (max_value // 200 + 1) * 200, 200))
click = alt.selection_single(encodings=['x'], nearest=True)
hist = alt.Chart(data).mark_bar().encode(
alt.X('Event Count:Q', bin=alt.Bin(maxbins=20), title='events (n)', axis=alt.Axis(values=ticks)),
alt.Y('count()', title='frequency (n)'),
tooltip=[alt.Tooltip('mean(Event Count):Q', title='Mean', format='.2f')]
).properties(
width=600,
height=400
).add_selection(
click
)
mean_rule = alt.Chart(data).transform_filter(
click
).mark_rule(color='firebrick', size=3).encode(
x='mean(Event Count):Q',
)
chart2 = alt.layer(hist, mean_rule).properties(
title='Histogram of Event Counts with Click Interaction and Tooltip'
)
chart2
combined_chart1 = alt.hconcat(
chart1,
chart2,
spacing=10
).resolve_scale(
color='independent'
)
combined_chart1
|