Skip to content

Commit

Permalink
Merge pull request #4 from maxibor/master
Browse files Browse the repository at this point in the history
fix viz for #3
  • Loading branch information
philippjfr authored Oct 12, 2019
2 parents d0157d0 + 99a9767 commit 2bb29de
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions gapminder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
"dataset = pd.read_csv(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset.sample(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -71,10 +80,12 @@
" xlabel = 'GDP per capita (2000 dollars)'\n",
" ylabel = 'Life expectancy (years)'\n",
" ylim = (20, 90)\n",
" \n",
"\n",
" def get_data(self):\n",
" df = dataset[(dataset.year==self.year) & (dataset.gdpPercap < 10000)].copy()\n",
" df['size'] = np.sqrt(df['pop']*2.666051223553066e-05)\n",
" self.xlim = (df['gdpPercap'].min()-100,df['gdpPercap'].max()+1000)\n",
" return df\n",
" \n",
" def mpl_view(self):\n",
Expand All @@ -83,11 +94,12 @@
" \n",
" plot = plt.figure(figsize=(7, 6))\n",
" ax = plot.add_subplot(111)\n",
" ax.set_xscale(\"log\", nonposx='clip')\n",
" ax.set_xscale(\"log\")\n",
" ax.set_title(title)\n",
" ax.set_xlabel(self.xlabel)\n",
" ax.set_ylabel(self.ylabel)\n",
" ax.set_ylim(self.ylim)\n",
" ax.set_xlim(self.xlim)\n",
"\n",
" for continent, df in data.groupby('continent'):\n",
" ax.scatter(df.gdpPercap, y=df.lifeExp, s=df['size']*5,\n",
Expand All @@ -106,7 +118,7 @@
" traces = []\n",
" for continent, df in data.groupby('continent'):\n",
" marker=dict(symbol='circle', sizemode='area', sizeref=0.1, size=df['size'], line=dict(width=2))\n",
" traces.append(go.Scatter(x=df.gdpPercap, y=df.lifeExp, mode='markers', marker=marker, name=continent))\n",
" traces.append(go.Scatter(x=df.gdpPercap, y=df.lifeExp, mode='markers', marker=marker, name=continent, text=df.country))\n",
" \n",
" axis_opts = dict(gridcolor='rgb(255, 255, 255)', zerolinewidth=1, ticklen=5, gridwidth=2)\n",
" layout = go.Layout(title=title, showlegend=self.show_legend, width=550,\n",
Expand All @@ -124,16 +136,17 @@
" alt.X('gdpPercap:Q', scale=alt.Scale(type='log'), axis=alt.Axis(title=self.xlabel)),\n",
" alt.Y('lifeExp:Q', scale=alt.Scale(zero=False, domain=self.ylim), axis=alt.Axis(title=self.ylabel)),\n",
" size=alt.Size('pop:Q', scale=alt.Scale(type=\"log\"), legend=None),\n",
" color=alt.Color('continent', scale=alt.Scale(scheme=\"category10\"), **legend))\\\n",
" color=alt.Color('continent', scale=alt.Scale(scheme=\"category10\"), **legend),\n",
" tooltip=['continent','country'])\\\n",
" .properties(title=title).configure_axis(grid=False)\n",
" return plot\n",
" return plot.interactive()\n",
" \n",
" def hvplot_view(self):\n",
" data = self.get_data()\n",
" title = \"hvPlot/Bokeh: \" + (self.title % self.year)\n",
" \n",
" plot = data.hvplot.scatter('gdpPercap', 'lifeExp', by='continent', s='size',\n",
" logx=True, title=title, width=500, height=400, legend=self.show_legend)\n",
" logx=True, title=title, width=500, height=400, legend=self.show_legend, hover_cols=['country'])\n",
" plot = plot.options(legend_position='bottom_right', xticks=[500, 1000, 2000, 5000, 10000])\n",
" plot = plot.redim.label(gdpPercap=self.xlabel, lifeExp=self.ylabel)\n",
" plot = plot.redim.range(lifeExp=self.ylim, gdpPercap=(200, 12000))\n",
Expand Down Expand Up @@ -223,7 +236,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2bb29de

Please sign in to comment.