-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_plot_maps.py
More file actions
107 lines (83 loc) · 3.59 KB
/
Copy pathdata_plot_maps.py
File metadata and controls
107 lines (83 loc) · 3.59 KB
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
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from pyspark.sql.functions import first
from ea_common import spark, filtered_rounds, db_path
# Filtered rounds needs to grouped up by user_id
filtered_rounds \
.groupBy("round_id") \
.agg(
first("user_id").alias("user_id"),
first("team").alias("team"),
first("result").alias("result"),
first("map_name").alias("map_name"),
first("server_name").alias("server_name"),
first("start_time").alias("start_time"),
first("end_time").alias("end_time")
).createOrReplaceTempView("filtered_rounds")
# Map balance:
# - Precomputed by data_analysis_maps.py (win percentage per map, filtered
# to classic ns_ maps with at least 3 recorded wins for each team) and
# written to db/map_balance. Just read and plot it here.
map_stats_pd = spark.read.parquet(db_path("map_balance")).toPandas()
# Set the map_name as the index for the DataFrame
map_stats_pd.set_index('map_name', inplace=True)
# Sort by marine win percentage, matching the previous ORDER BY
map_stats_pd.sort_values('marine_win_percentage', ascending=False, inplace=True)
map_stats_pd = map_stats_pd.rename(columns={'marine_win_percentage': 'Marine Win %',
'alien_win_percentage': 'Alien Win %'})
# Create a stacked bar chart with percentages
map_stats_pd[['Marine Win %', 'Alien Win %']].plot(kind='barh',
stacked=True,
color=['#0492C2', '#FCAE1E'])
# Add labels and title
plt.xlabel('Map Name')
plt.ylabel('Win Percentage')
plt.title('Marine and Alien Win Percentage by Map')
# Set the x-axis ticks
plt.xticks(np.arange(0, 101, 10))
# Add a vertical line at the 50% mark
plt.axvline(x=50, color='r', linestyle='dotted')
# Map popularity:
# - Calculate the total games for each map
# - Include only classic ns maps (ns_ prefix)
map_popularity_df = spark.sql("""
SELECT
LOWER(r.map_name) as map_name,
COUNT(*) as total_games
FROM
filtered_rounds r
WHERE
r.map_name IS NOT NULL AND
LOWER(r.map_name) != 'none' AND
LOWER(r.map_name) LIKE 'ns_%'
GROUP BY
LOWER(r.map_name)
ORDER BY
total_games DESC
""")
map_popularity_df.show()
# Convert the Spark DataFrame to a Pandas DataFrame
map_popularity_pd = map_popularity_df.toPandas()
# Set the map_name as the index for the DataFrame
map_popularity_pd.set_index('map_name', inplace=True)
# Calculate the percentage of total games for each map
map_popularity_pd['percentage'] = map_popularity_pd['total_games'] / map_popularity_pd['total_games'].sum() * 100
# Create a new DataFrame for maps with less than 5% of total games
other_maps = map_popularity_pd[map_popularity_pd['percentage'] < 1.5].sum()
# Remove these maps from the original DataFrame
map_popularity_pd = map_popularity_pd[map_popularity_pd['percentage'] >= 1.5]
# Add the new DataFrame to the original DataFrame
map_popularity_pd = map_popularity_pd._append(pd.DataFrame({'total_games': other_maps['total_games'],
'percentage': other_maps['percentage']},
index=['Other maps']))
# Generate a color map with as many colors as there are data points
cmap = plt.get_cmap('Paired')
colors = cmap(np.linspace(0, 1, len(map_popularity_pd)))
# Create a pie chart with the color map
plt.figure(figsize=(10, 5))
map_popularity_pd['total_games'].plot(kind='pie', autopct='%1.1f%%', colors=colors)
# Some tuning
plt.ylabel('')
plt.title('Map Popularity')
plt.show()