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
{
"cells": [
{
"cell_type": "markdown",
"id": "e3a23a06-adc3-4a7a-a9f9-d52052fe6a2b",
"metadata": {},
"source": [
"# Trying to get datashaded/rasterized hvPlots to show a legend\n",
"\n",
"This example uses an hvplot sample data set. As this sample dataset is actually quite small (~ 1 million records) datashading/rasterizing is not very beneficial. HOWEVER, in the case that you have 100 million or more datapoints, datashade/raster is required to generate a plot without crashing the browser (at least in my experience)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aae7024d-701b-40cf-9d2c-a07284a9be6b",
"metadata": {},
"outputs": [],
"source": [
"# load hvplot and default bokeh extension\n",
"import hvplot.pandas\n",
"import holoviews as hv\n",
"# load some sample data\n",
"import hvplot.sample_data\n",
"# pandas!\n",
"import pandas as pd\n",
"# colormaps\n",
"import colorcet as cc"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e4ba4f1-0af8-4cc4-a607-ce89d4e29ac1",
"metadata": {},
"outputs": [],
"source": [
"df = hvplot.sample_data.airline_flights.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb5424d0-3bba-431b-8916-ca717ccea969",
"metadata": {},
"outputs": [],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b82adb1b-e8ae-4e20-9e60-e136a201d9c8",
"metadata": {},
"outputs": [],
"source": [
"df['dayofweek'] = df.dayofweek.astype('category')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae8c032e-2cf5-4e00-b3a4-c029393b40f5",
"metadata": {},
"outputs": [],
"source": [
"df[df.carrier=='OO'].dayofweek.unique()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b56603a2-2969-468e-9240-120a789f249c",
"metadata": {},
"outputs": [],
"source": [
"df[df.carrier=='OO'].hvplot.scatter(\n",
" x='arr_time',\n",
" y='dep_time',\n",
" by='dayofweek'\n",
") + df[df.carrier=='OO'].hvplot.scatter(x='arr_time',\n",
" y='dep_time',\n",
" by='dayofweek',\n",
" datashade=True\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "e3ee18c7-99b0-4efc-95fd-cc8946e8b2cd",
"metadata": {},
"source": [
"## Here's a hack I figured out to have a legend and a datashaded plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32263f26-10fd-4d0b-8d35-1c138a82b943",
"metadata": {},
"outputs": [],
"source": [
"arr = []\n",
"for i in range(1, len(df.dayofweek.unique()) + 1):\n",
" arr.append(df[(df.carrier=='OO') & (df.dayofweek == i)].head(1))\n",
"minidf = pd.concat(arr)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59b31f83-748f-4e12-8ddb-fb7369615856",
"metadata": {},
"outputs": [],
"source": [
"minidf.hvplot.scatter(\n",
" x='arr_time',\n",
" y='dep_time',\n",
" by='dayofweek',\n",
" s=1\n",
") * df[df.carrier=='OO'].hvplot.scatter(x='arr_time',\n",
" y='dep_time',\n",
" by='dayofweek',\n",
" datashade=True\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "46e055ee-0fb9-484b-b7a9-ad4dd9908fc1",
"metadata": {},
"source": [
"## Recommended to try rasterize to see if that would generate a legend"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b660bfb1-e630-4461-89b5-a711322e1088",
"metadata": {},
"outputs": [],
"source": [
"by_plot = None\n",
"by_plot = df[df.carrier=='OO'].hvplot.scatter(x='arr_time',\n",
" y='dep_time',\n",
" by='dayofweek',\n",
" rasterize=True\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0b13940-0dac-4af0-b335-ab5c62b05219",
"metadata": {},
"outputs": [],
"source": [
"by_plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a8a6b93-82a2-4e95-b883-8493bd883ce5",
"metadata": {},
"outputs": [],
"source": [
"print(by_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bdb799c7-6cb3-4a55-8de1-b7830fd40983",
"metadata": {},
"outputs": [],
"source": [
"type(by_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0466c9d4-3491-40c0-bec5-faa8115d5950",
"metadata": {},
"outputs": [],
"source": [
"hv.help(by_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fb5f856-ddec-4955-8a8e-13c5e3ce8994",
"metadata": {},
"outputs": [],
"source": [
"by_plot.opts('Image', color_levels=7)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:waveform] *",
"language": "python",
"name": "conda-env-waveform-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.11.6"
},
"nteract": {
"version": "0.28.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment