Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix viz for #3 #4

Merged
merged 3 commits into from
Oct 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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