update Bars example to demo dataframe constructor
"outputs": [],
"source": [
"import numpy as np\n",
+ "import pandas as pd\n",
"import holoviews as hv\n",
"hv.extension('bokeh')"
]
"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": {},
"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:"
]
},
{
"\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)"
]
"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."
]
},
"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)``."
]
}
],
}
},
"nbformat": 4,
- "nbformat_minor": 2
+ "nbformat_minor": 4
}
