Skip to content

Commit

Permalink
Expand colour scale example
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklupton committed Feb 14, 2023
1 parent 9376781 commit 21abc95
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions docs/tutorials/colour-scales.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, if we want to make the colour of the flow to be proportional to a numerical value. Use the `hue` parameter to set the name of the variable that you want to display in colour. To start off, let's use \"value\", which is the width of the lines: wider lines will be shown in a darker colour."
"Now, if we want to make the colour of the flow to be proportional to a numerical value."
]
},
{
Expand Down Expand Up @@ -290,10 +290,10 @@
" super().__init__(attr)\n",
" self.threshold = threshold\n",
" \n",
" def get_color(self, link, value):\n",
" if value < self.threshold:\n",
" def get_color(self, link, normalised_value):\n",
" if normalised_value < self.threshold:\n",
" return '#ddd'\n",
" return super().get_color(link, value)"
" return super().get_color(link, normalised_value)"
]
},
{
Expand Down Expand Up @@ -330,6 +330,87 @@
" .to_widget(**size_options)\n",
" w.links = w_new.links"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This colour scale decides whether to choose a grey colour based on the normalised value (within a range of 0 to 1) which is used to lookup a colour in the colour scale.\n",
"\n",
"Alternatively, you could intervene based on the absolute value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class DimmingScaleAbsolute(QuantitativeScale):\n",
" def __init__(self, attr, threshold, **kwargs):\n",
" super().__init__(attr)\n",
" self.threshold = threshold\n",
" \n",
" def __call__(self, link, measures):\n",
" value = self.get_value(link, measures)\n",
" if value < self.threshold:\n",
" return '#ddd'\n",
" return super().__call__(link, measures)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_scale3 = DimmingScaleAbsolute('Calories Burnt', threshold=2, palette='Blues_9')\n",
"weave(sdd, dataset, measures='Calories Burnt', link_color=my_scale3) \\\n",
" .to_widget(**size_options)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A similar approach can be used with a `CategoricalScale` as well as a `QuantitativeScale`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class DimmingCategoricalScale(CategoricalScale):\n",
" def __init__(self, attr, threshold_measure, threshold_value, **kwargs):\n",
" \"\"\"Acts like CategoricalScale unless threshold_measure is below threshold_value.\"\"\"\n",
" super().__init__(attr)\n",
" self.threshold_measure = threshold_measure\n",
" self.threshold_value = threshold_value\n",
" \n",
" def __call__(self, link, measures):\n",
" value = measures[self.threshold_measure]\n",
" if value < self.threshold_value:\n",
" return '#ddd'\n",
" return super().__call__(link, measures)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_scale3 = DimmingCategoricalScale(\n",
" 'type',\n",
" threshold_measure='Calories Burnt', \n",
" threshold_value=6, \n",
" palette='Blues_9'\n",
")\n",
"weave(sdd, dataset, measures='Calories Burnt', link_color=my_scale3) \\\n",
" .to_widget(**size_options)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 21abc95

Please sign in to comment.