Skip to content

Instantly share code, notes, and snippets.

@stringertheory
Created March 14, 2014 15:14
Show Gist options
  • Save stringertheory/9549661 to your computer and use it in GitHub Desktop.
Save stringertheory/9549661 to your computer and use it in GitHub Desktop.
A quicky script to make a vincent stacked area plot with the results of the threes simulation [this blog post](http://datascopeanalytics.com/what-we-think/2014/03/14/are-you-really-any-good-at-threes).
# standard library
import sys
import random
# 3rd party
import numpy as np
import vincent
N_SAMPLES = 10000
# read from stdin
score_distribution = [int(line) for line in sys.stdin]
# what is the expected random-swiper high score, given the number of
# games played?
vincent_data = {
'index': [],
'y1': [],
'y2': [],
'y3': [],
}
for n_games_played in range(1, 21):
high_score_distribution = []
for i in range(N_SAMPLES):
high_score = max(random.choice(score_distribution)
for j in xrange(n_games_played))
high_score_distribution.append(high_score)
# calculate sizes needed for stacked area
lower = np.percentile(high_score_distribution, 5)
size = np.percentile(high_score_distribution, 95) - lower
top = 1400 - (lower + size)
# do this to get in correct vincent format
vincent_data['index'].append(n_games_played)
vincent_data['y1'].append(lower)
vincent_data['y2'].append(size)
vincent_data['y3'].append(top)
# make the chart
stacked = vincent.StackedArea(vincent_data, iter_idx='index')
stacked.colors('PuOr')
stacked.axis_titles(x='games played', y='high score')
stacked.to_json('stacked.json', html_out=True, html_path='index.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment