Skip to content

Instantly share code, notes, and snippets.

@algmyr
Last active April 25, 2019 23:18
Show Gist options
  • Save algmyr/f12915c18421d283a6ee6f29eed0f75c to your computer and use it in GitHub Desktop.
Save algmyr/f12915c18421d283a6ee6f29eed0f75c to your computer and use it in GitHub Desktop.
import json
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.lines as mlines
import seaborn as sns
sns.set(style="dark")
# Data
with open('ratingdata.json') as f:
data = json.load(f)
ratings = np.array(sorted(user['rating'] for user in data['result']))
n = len(ratings)
perc = 100*np.arange(n)/n
# Rating info
cutoffs = [ratings[0],1200, 1400,1600,1900,2100,2300,2400,2600,3000,ratings[-1]]
colors = [
'#cccccc', '#77ff77', '#77ddbb', '#aaaaff', '#ff88ff',
'#ffcc88', '#ffbb55', '#ff7777', '#ff3333', '#aa0000',
]
intervals = [(cutoffs[i], cutoffs[i+1]) for i in range(len(cutoffs)-1)]
# Plot
fig,ax = plt.subplots(1)
ax.plot(ratings, perc, color='#00000099')
# Rects
def fill_interval(interval, color):
alpha = '99'
l,r = interval
col = color + alpha
rect = patches.Rectangle((l,-50), r-l, 200, edgecolor='none', facecolor=col)
ax.add_patch(rect)
for interval,color in zip(intervals,colors):
fill_interval(interval, color)
# Lines
for y in range(0,101,20):
l = mlines.Line2D([cutoffs[0],cutoffs[-1]], [y,y], color='#00000022')
ax.add_line(l)
# General
plt.xlim(ratings[0], ratings[-1])
plt.ylim(-1.5, 101.5)
plt.title('Percentile vs rating')
plt.xlabel('Rating')
plt.ylabel('Percentile')
plt.xticks([])
#plt.xticks(cutoffs[1:-1], rotation=45)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment