From 10d5048856d44a09494ef309ae776f06a2f3669e Mon Sep 17 00:00:00 2001 From: Kim Pevey Date: Thu, 22 Oct 2020 09:15:16 -0500 Subject: [PATCH] update Bars example to demo dataframe constructor --- examples/reference/elements/bokeh/Bars.ipynb | 48 +++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/examples/reference/elements/bokeh/Bars.ipynb b/examples/reference/elements/bokeh/Bars.ipynb index b46630ebef..fd524d7ce2 100644 --- a/examples/reference/elements/bokeh/Bars.ipynb +++ b/examples/reference/elements/bokeh/Bars.ipynb @@ -18,6 +18,7 @@ "outputs": [], "source": [ "import numpy as np\n", + "import pandas as pd\n", "import holoviews as hv\n", "hv.extension('bokeh')" ] @@ -44,6 +45,16 @@ "bars" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can achieve the same plot using a Pandas DataFrame:\n", + "```\n", + "hv.Bars(pd.DataFrame(data, columns=['Car occupants','Count']))\n", + "```" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -84,7 +95,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "``Bars`` support nested categorical groupings, e.g. here we will create a random sample of pets sub-divided by male and female:" + "``Bars`` also supports nested categorical groupings. Next we'll use a Pandas DataFrame to construct a random sample of pets sub-divided by male and female:" ] }, { @@ -100,16 +111,28 @@ "\n", "pets_sample = np.random.choice(pets, samples)\n", "gender_sample = np.random.choice(genders, samples)\n", + "count = np.random.randint(1, 5, size=samples)\n", "\n", - "bars = hv.Bars((pets_sample, gender_sample, np.ones(samples)), ['Pets', 'Gender']).aggregate(function=np.sum)\n", + "df = pd.DataFrame({'Pets': pets_sample, 'Gender': gender_sample, 'Count': count})\n", + "df.head(2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bars = hv.Bars(df, kdims=['Pets', 'Gender']).aggregate(function=np.sum)\n", "\n", - "bars.opts(width=500)" + "bars.opts(width=800)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "Just as before we can provide an explicit ordering by declaring the `Dimension.values`. Alternatively we can also make use of the `.sort` method, internally `Bars` will use topological sorting to ensure consistent ordering." ] }, @@ -158,16 +181,29 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "For full documentation and the available style and plot options, use ``hv.help(hv.Bars).``" + "For full documentation and the available style and plot options, use ``hv.help(hv.Bars)``." ] } ], "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", "name": "python", - "pygments_lexer": "ipython3" + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }