Skip to content

Instantly share code, notes, and snippets.

@empet
Created July 16, 2014 04:38
Show Gist options
  • Save empet/df423281dbc1a4053f6b to your computer and use it in GitHub Desktop.
Save empet/df423281dbc1a4053f6b to your computer and use it in GitHub Desktop.
A comparative Visualization of Ranking Position of Russia, USA and China at International Mathematical Olympiad.
{
"metadata": {
"name": "",
"signature": "sha256:02bf25459b7bfe3092f81b30c7c72698d9de8cdff4a24467968656e82f5c70f2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Ranking positions of Russia+USSR, USA, and China at International Mathematical Olympiads "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the table of results from IMO's site:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"url=r'http://www.imo-official.org/results.aspx'\n",
"dfs = pd.read_html(url)#returns a list of tables\n",
"#dfs[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tab=dfs[0].values\n",
"print tab.shape"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(123L, 57L)\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"years=[i for i in range(1959, 1980)]+ [j for j in range(1981, 2015)]\n",
"#IMO was not held in 1980"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Rus= tab[91,1:56] #extract information for Russia; after 1990\n",
"\n",
"USSR=tab[117,1:56] # until 1990\n",
"\n",
"Rus[23:]=USSR[23:]\n",
"RusUrs=Rus[::-1]#reverts values such that they correspond to years starting with 1959\n",
"RusUrs[1]=0.0#replace nan values \n",
"RusUrs[2]=0.0\n",
"RusUrs[19]=0.0\n",
"RusUrs[31]=1.0\n",
"RusUrs[32]=6.0\n",
"RusUrs[50]=2.0\n",
"RusUrs=RusUrs.astype(int)\n",
"\n",
"L=len(years)\n",
"I=[i for i in range(L)]\n",
"for i in I:\n",
" IR=filter(lambda i: RusUrs[i] !=0, I)\n",
" \n",
"yearR=[years[i] for i in IR]\n",
"RusUrsP=[RusUrs[i] for i in IR]\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rUSA= tab[116,1:56] #extract information for SUA\n",
"\n",
"USA=rUSA[::-1]\n",
"\n",
"USA[:15]=0.0\n",
"USA[31]=5.0\n",
"USA[32]=2.0\n",
"USA[50]=3.0\n",
"USA=USA.astype(int)\n",
"\n",
"for i in I:\n",
" IUSA=filter(lambda i: USA[i] !=0, I)\n",
"yearUSA=[years[i] for i in IUSA]\n",
"USAP=[USA[i] for i in IUSA]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rCHI= tab[20,1:56] #extract information for China\n",
"rCHI[4]=1.0\n",
"rCHI[16]=0\n",
"rCHI[22]=1.0\n",
"rCHI[23]=2.0\n",
"\n",
"rCHI[30:]=0.0\n",
"\n",
"CHI=rCHI[::-1].astype(int)\n",
"for i in I:\n",
" IC=filter(lambda i: CHI[i] !=0, I)\n",
"yearC=[years[i] for i in IC]\n",
"CHIP=[CHI[i] for i in IC]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import seaborn as sb\n",
"colors=sb.color_palette()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import plotly.plotly as ply\n",
"ply.sign_in('empet','my_api_key')\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mpl_fig_obj= plt.figure()\n",
"plt.rcParams['figure.figsize'] = (12.0, 7.0)\n",
"\n",
"plt.plot(yearR, RusUrsP, color=colors[0], marker='o', label='USSR+Russia')\n",
"plt.plot(yearUSA, USAP, color=colors[1], marker='o', label='USA')\n",
"plt.plot(yearC, CHIP, color=colors[2], marker='o', label='CHINA')\n",
"\n",
"plt.title(\"Positions of Russia, USA and China in the score ordering at IMOs, between 1959-2014\")\n",
"plt.xlabel('Year')\n",
"plt.ylabel('Rank')\n",
"#plt.legend(loc='upper left')\n",
"plt.axis([1958,2015,-1,35])\n",
"plt.xticks([i for i in range(1960, 2015, 5)])\n",
"plt.yticks([i for i in range(0, 33, 2)])\n",
"ply.iplot_mpl(mpl_fig_obj, filename='IMORankingRu_USA_CHI')\n",
"#plt.savefig('RusUSACHIatIMOs.jpg', dpi=600)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~empet/19\" height=\"525\" width=\"100%\"></iframe>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x17768400>"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"E Petrisor, UPT"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"def css_styling():\n",
" styles = open(\"./styles/custom.css\", \"r\").read()\n",
" return HTML(styles)\n",
"css_styling()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<style>\n",
" @font-face {\n",
" font-family: \"Computer Modern\";\n",
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
" }\n",
" div.cell{\n",
" width:800px;\n",
" margin-left:16% !important;\n",
" margin-right:auto;\n",
" }\n",
" h1 {\n",
" font-family: Helvetica, serif;\n",
" }\n",
" h4{\n",
" margin-top:12px;\n",
" margin-bottom: 3px;\n",
" }\n",
" div.text_cell_render{\n",
" font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
" line-height: 145%;\n",
" font-size: 130%;\n",
" width:800px;\n",
" margin-left:auto;\n",
" margin-right:auto;\n",
" }\n",
" .CodeMirror{\n",
" font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n",
" }\n",
" .prompt{\n",
" display: None;\n",
" }\n",
" .text_cell_render h5 {\n",
" font-weight: 300;\n",
" font-size: 22pt;\n",
" color: #4057A1;\n",
" font-style: italic;\n",
" margin-bottom: .5em;\n",
" margin-top: 0.5em;\n",
" display: block;\n",
" }\n",
" \n",
" .warning{\n",
" color: rgb( 240, 20, 20 )\n",
" } \n",
"</style>\n",
"<script>\n",
" MathJax.Hub.Config({\n",
" TeX: {\n",
" extensions: [\"AMSmath.js\"]\n",
" },\n",
" tex2jax: {\n",
" inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
" displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
" },\n",
" displayAlign: 'center', // Change this to 'center' to center equations.\n",
" \"HTML-CSS\": {\n",
" styles: {'.MathJax_Display': {\"margin\": 4}}\n",
" }\n",
" });\n",
"</script>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"<IPython.core.display.HTML at 0x17768a58>"
]
}
],
"prompt_number": 16
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment