Created
May 17, 2017 14:11
-
-
Save xjcl/eddf9527ea114af84a1f82d089cf9d06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 matplotlib.pyplot as plt; plt.rcdefaults() | |
import numpy as np | |
import matplotlib.pyplot as plt | |
data = [ | |
['India', 16], | |
['United States', 56], | |
['Bangladesh', 57], | |
['France', 71], | |
['Brazil', 83], | |
['Indonesia', 90], | |
['Romania', 92], | |
['Germany', 94], | |
['Australia', 97], | |
['Canada', 98], | |
['Thailand', 113], | |
['Hungary', 137], | |
['Hong Kong', 143], | |
['Netherlands', 169], | |
['Vietnam', 177], | |
['Poland', 180], | |
['Taiwan', 194], | |
['Iran', 207], | |
['Ukraine', 214], | |
['China', 224], | |
['Belarus', 241], | |
['Slovakia', 252], | |
['Japan', 263], | |
['Russia', 279], | |
] | |
c = ['China', 'Taiwan', 'Singapore', 'Macau', 'Hong Kong'] | |
cjk = c + ['Japan'] + ['South Korea'] + ['Vietnam'] | |
slavs = ['Russia', 'Poland', 'Ukraine', 'Serbia', 'Czech Republic', 'Bulgaria', 'Belarus', 'Croatia', 'Slovakia', 'Bosnia', 'Slovenia', 'Macedonia', 'Montenegro'] | |
west = ['United States', 'Canada', 'Australia', 'New Zealand', 'Germany', 'France', 'Spain', 'Italy', 'United Kingdom', 'Portugal', 'Austria', 'Switzerland', 'Netherlands', 'Belgium', 'Luxembourg', 'Denmark', 'Norway', 'Sweden', 'Finland'] | |
# plus some microstates, whatever | |
def get_color(c): | |
if c in cjk: | |
return ['Sinosphere', 'gold'] | |
if c in slavs: | |
return ['Slavic countries', 'lightskyblue'] | |
if c in west: | |
return ['Western countries', 'lightcoral'] | |
# return 'white' | |
return ['Other countries', 'black'] | |
data_x = [d[0] for d in data] | |
data_y = [d[1]/10 for d in data] | |
colors = [get_color(d[0]) for d in data] | |
y_pos = np.arange(len(data)) | |
fig = plt.gcf() | |
# fig.set_size_inches(18.5, 10.5) | |
# fig.set_size_inches(10, 4) | |
# fix ugly default layout | |
ax = plt.axes() | |
ax.xaxis.tick_top() | |
ax.xaxis.set_ticks_position('none') | |
ax.yaxis.set_ticks_position('none') | |
ax.xaxis.set_label_position('top') | |
ax.spines['top'].set_visible(False) | |
ax.spines['bottom'].set_visible(False) | |
ax.spines['right'].set_visible(False) | |
ax.spines['left'].set_visible(False) | |
pos1 = ax.get_position() # get the original position | |
pos2 = [pos1.x0, pos1.y0 + .07, pos1.width, pos1.height] | |
ax.set_position(pos2) | |
# plt.barh(y_pos, data_y, color=colors, align='center', label=colors, linewidth=0, alpha=0.5) | |
for i in range(len(data)): | |
if data_x[i] in ['Netherlands', 'Vietnam', 'Poland', 'Iran']: | |
# dum stupid hack for legend | |
# there seems to be no way to set it manually :/ | |
plt.barh(i, data_y[i], color=colors[i][1], align='center', label=colors[i][0], linewidth=0, alpha=0.5) | |
else: | |
plt.barh(i, data_y[i], color=colors[i][1], align='center', linewidth=0, alpha=0.5) | |
plt.yticks(y_pos, data_x) | |
# plt.xlabel('percentage of qualification round participants advancing to round 2') | |
plt.xlabel('percentage of round 2 participants vs qualification round participants') | |
FIGURE_TITLE = '2015 Google Code Jam Round 2 advancement*, by country' | |
# plt.title(FIGURE_TITLE) | |
plt.text(1.4, 29.3, FIGURE_TITLE, | |
# horizontalalignment='center', | |
fontsize=14) | |
# fontsize=20, | |
# transform = ax2.transAxes) | |
# plt.show() | |
# problems | |
# 1. unable to get the xticks (0,5,..) close to the data ! | |
# 2. HUGE white space below india => hide by moving xticks to top, | |
# hiding opposite axis | |
# 3. ugly legend hack | |
# 4. maybe use flags? probably won't be able to get it to work in matplotlib.. | |
# 5. data old and not nicely collected, better use pure advancement rate | |
# workaround: manually move in inkscape | |
plt.legend(loc="lower right", frameon=False) | |
plt.tight_layout() | |
# plt.savefig('out.svg', bbox_inches='tight') | |
plt.savefig('out.svg') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment