Skip to content

Commit

Permalink
Add example of grey colour scale below threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklupton committed Feb 9, 2023
1 parent 10dada0 commit 9376781
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions docs/tutorials/colour-scales.ipynb
Expand Up @@ -19,9 +19,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
Expand Down Expand Up @@ -58,9 +56,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"partition_job = Partition.Simple('Employment Job', np.unique(df1['Employment Job']))\n",
Expand All @@ -77,9 +73,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# these statements or the ones above do the same thing\n",
Expand All @@ -97,9 +91,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"nodes = {\n",
Expand Down Expand Up @@ -252,9 +244,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"class MyScale(QuantitativeScale):\n",
Expand All @@ -281,11 +271,70 @@
" link_color=my_scale) \\\n",
" .to_widget(**size_options)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, maybe you want to hide the smallest flows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class DimmingScale(QuantitativeScale):\n",
" def __init__(self, attr, threshold, **kwargs):\n",
" super().__init__(attr)\n",
" self.threshold = threshold\n",
" \n",
" def get_color(self, link, value):\n",
" if value < self.threshold:\n",
" return '#ddd'\n",
" return super().get_color(link, value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_scale2 = DimmingScale('Calories Burnt', threshold=0.3, palette='Blues_9')\n",
"w = weave(sdd, dataset, measures='Calories Burnt', link_color=my_scale2) \\\n",
" .to_widget(**size_options)\n",
"w"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just for fun, you can adjust the threshold interactively:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact\n",
"\n",
"@interact(threshold=(0.0, 1.0, 0.1))\n",
"def update_threshold(threshold=0.3):\n",
" my_scale2.threshold = threshold\n",
" w_new = weave(sdd, dataset, measures='Calories Burnt', link_color=my_scale2) \\\n",
" .to_widget(**size_options)\n",
" w.links = w_new.links"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +348,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 9376781

Please sign in to comment.