Skip to content

Instantly share code, notes, and snippets.

@magic-lantern
Last active December 2, 2023 03:11
Show Gist options
  • Save magic-lantern/7cc4bd51425b74f66a021665ede0a688 to your computer and use it in GitHub Desktop.
Save magic-lantern/7cc4bd51425b74f66a021665ede0a688 to your computer and use it in GitHub Desktop.
name: hvplot_example
channels:
- conda-forge
- defaults
dependencies:
- python=3.10
- hvplot
- intake
- intake-parquet
- intake-xarray
- s3fs
- datashader
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "f5dbf693-e787-461e-8990-b5a980f3877b",
"metadata": {},
"source": [
"## Example of customizing subplots with hvPlot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0305d30-ada1-4f5a-a382-3645f59c6e5d",
"metadata": {},
"outputs": [],
"source": [
"# load hvplot and default bokeh extension\n",
"import hvplot.pandas\n",
"# load some sample data\n",
"import hvplot.sample_data\n",
"# colormaps\n",
"import colorcet as cc"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99dc368c-4266-48c5-860c-9f8b9321400e",
"metadata": {},
"outputs": [],
"source": [
"df = hvplot.sample_data.airline_flights.read()"
]
},
{
"cell_type": "markdown",
"id": "cb83d52e-695a-4765-b7c9-c5f0defff408",
"metadata": {},
"source": [
"Notice how each day of the week automatically is assigned a unique color?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41ada7de-9c3e-499e-b79c-1df387d91a73",
"metadata": {},
"outputs": [],
"source": [
"df.hvplot.hist('arr_time',\n",
" by='dayofweek'\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "9d2332dd-d396-4d26-9e7c-cb7424f4b305",
"metadata": {},
"source": [
"Now when plotting each day of the week on their own, colors are gone and we're back to the default blue.\n",
"\n",
"Where did my colors go? How to preserve them?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8c1dd91-8f49-41ef-aa6d-50aaf037e065",
"metadata": {},
"outputs": [],
"source": [
"df.hvplot.hist('arr_time',\n",
" by='dayofweek',\n",
" subplots=True,\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "280cf4d3-c5e1-49bd-be98-b0b47b0f8977",
"metadata": {},
"source": [
"It is possible to build each individual day of the week plot and color each one individually."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0efa909c-09ae-4036-a5b9-61399c43c97a",
"metadata": {},
"outputs": [],
"source": [
"all_p = None\n",
"for dow, c in zip(df.dayofweek.unique(), cc.b_glasbey_bw_minc_20[:len(df.dayofweek.unique())]):\n",
" p = df[df.dayofweek == dow].hvplot.hist('arr_time',\n",
" by='dayofweek',\n",
" color=c,\n",
" title='dayofweek: ' + str(dow)\n",
" )\n",
" all_p = p if all_p is None else all_p + p\n",
"all_p.cols(2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:py310]",
"language": "python",
"name": "conda-env-py310-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment