{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
" # Kqlmagic - __palette__ features\n",
"***\n",
"Explains how to customize and use Kqlmagic **palette** features\n",
"***\n",
"***\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make sure that you have the lastest version of Kqlmagic\n",
"Download Kqlmagic from PyPI and install/update\n",
"(if latest version ims already installed you can skip this step)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!pip install Kqlmagic --no-cache-dir --upgrade"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add Kqlmagic to notebook magics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%reload_ext Kqlmagic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authenticate to get access to data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql AzureDataExplorer://code;cluster='help';database='Samples'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Query and render to piechart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql\n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use palette Reds for the piechart\n",
"- set option -palette_name or -pn to Reds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_name \"Reds\"\n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - ### *Note - to set a different palette, use option -palette_name*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use desaturated Reds for the piechart\n",
"- set option -palette_desaturation or -pd with a value between 0 to 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_name \"Reds\" -palette_desaturation 0.5 \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Parametrized palette and desaturation\n",
"- set python variables to the palette and distaturioan value, and refer them from the magic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_palettes = [\"Reds\", \"Greens\", \"Blues\"]\n",
"my_saturation = 0.7\n",
"current_palette = 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_name my_palettes[current_palette] -palette_desaturation my_saturation \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"current_palette = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_name my_palettes[current_palette] -palette_desaturation my_saturation \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Show current default palette "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### palette desaturated by half (0.5)\n",
"- -pd is an abreviation of -palette_desaturation\n",
"- desturation value must be between to 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pd 0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - ### *Note - desaturation value is explicitly specified only if it is less than 1*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### palette extended to 40 colors\n",
"- -pc is an abreviation of palettte_colors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pd 0.5 -pc 40"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### alternative Reds palette, desaturated by 0.9, 40 colors \n",
"- Beware that palettes are case sensitive"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"Reds\" -pd 0.5 -pc 40"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Builtin palettes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### popup all builtin palettes\n",
"- push the button to open a popup window with the palettes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palettes -popup_window"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show all builtin palettes desaturated\n",
"- set option -palette_desaturation or -pd with a value between 0 to 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%kql --palettes -palette_desaturation 0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### popup all builtin n_colors palettes desaturated\n",
"- set option -palette_colrs or -pc with a value greater than 0\n",
"- set option -palette_desaturation or -pd with a value between 0 to 1\n",
"- push the button to open a popup window with the palettes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palettes -pd 0.5 -pc 20 -pw"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configure default palette properties\n",
"- palette_name\n",
"- palette_desaturation\n",
"- palette_colors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show palette name of the default palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### modify the default palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_name = 'Greens'\n",
"%config Kqlmagic.palette_name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show palette desaturation default"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_desaturation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### modify palette desaturation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_desaturation = 0.95\n",
"%config Kqlmagic.palette_desaturation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show number of colors in default palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_colors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### modify number of colors in default palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_colors = 6\n",
"%config Kqlmagic.palette_colors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show the deault palette based on the new defaults"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_desaturation 0.5 \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - ### *Note - The palette size extends automativally, if required by chart (in above chart extended from 6 colors palette to 10 colors palette*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reverse color order of palette\n",
"- Set option -palette_reverse / -pr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -palette_reverse \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 10 \n",
" | render piechart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_colors = 20\n",
"%config Kqlmagic.palette_colors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Palette derivative names\n",
"- basic names\n",
"- subset names\n",
"- reversed names\n",
"- custom"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### basic pastel palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"pastel\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - ### *Note - When not specified, palette_name, palette_colors and palette_desaturation default properties are used*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### subset derived from a basic palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"pastel[4:11]\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### reversed from a basic palette "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"pastel_r\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### reversed subset of a basic palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"pastel[4:11]_r\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## piechart rendered using a derived palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -pn \"pastel[4:11]_r\" \n",
" StormEvents \n",
" | summarize count() by State\n",
" | sort by count_ \n",
" | limit 7 \n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### config default palette with a derived palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_name = \"pastel[4:11]_r\"\n",
"%config Kqlmagic.palette_name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### barchart rendered using the new default palette "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql \n",
" StormEvents \n",
" | summarize count() by State\n",
" | extend count2 = count_*1.4 \n",
" | extend count3 = count_*2.4\n",
" | sort by count_ \n",
" | limit 7 \n",
" | render barchart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom palette\n",
"- define an alternative basic palette\n",
"- have same characteristics as basic palette, a subset can be derived and/or reversed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show custom palette\n",
"- make sure that there are no white spaces in the string that defines the custom palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"['rgb(127,155,173)','rgb(103,135,156)','rgb(82,114,140)','rgb(63,93,122)','rgb(48,73,102)']\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show derived subset palette from the custom palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"['rgb(127,155,173)','rgb(103,135,156)','rgb(82,114,140)','rgb(63,93,122)','rgb(48,73,102)'][1:4]\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### show derived reversed subset palette from custom palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --palette -pn \"['rgb(127,155,173)','rgb(103,135,156)','rgb(82,114,140)','rgb(63,93,122)','rgb(48,73,102)'][1:4]_r\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### columnchart rendered using a custom palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -pn \"['rgb(127,155,173)','rgb(103,135,156)','rgb(82,114,140)','rgb(63,93,122)','rgb(48,73,102)'][1:4]_r\"\n",
" StormEvents \n",
" | summarize count() by State\n",
" | extend count2 = count_*1.4 \n",
" | extend count3 = count_*2.4\n",
" | sort by count_ \n",
" | limit 7 \n",
" | render columnchart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### set custom palette as default palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%config Kqlmagic.palette_name = \"['rgb(127,155,173)','rgb(103,135,156)','rgb(82,114,140)','rgb(63,93,122)','rgb(48,73,102)'][1:4]_r\"\n",
"%config Kqlmagic.palette_name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### barchart rendered using default palette (set to the custom chart)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql \n",
" StormEvents \n",
" | summarize count() by State\n",
" | extend count2 = count_*1.4 \n",
" | extend count3 = count_*2.4\n",
" | sort by count_ \n",
" | limit 7 \n",
" | render barchart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python integration"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kql -pn \"Spectral\" -pd 0.95 -pc 10\n",
" StormEvents \n",
" | summarize count() by State\n",
" | extend count2 = count_*1.4 \n",
" | extend count3 = count_*2.4\n",
" | sort by count_ \n",
" | limit 10\n",
" | render piechart"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get last kql result palette "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palette"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get a slice from palette\n",
"- behave as list of colors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palette[3:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get one color from palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palette[7]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get the palette raw rgb data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(_kql_raw_result_.palette)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get palette_name (name of the palette)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.options['palette_name']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get palette_colors value (number of colors in palette)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.options['palette_colors']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get palette_desaturation value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.options['palette_desaturation']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get palette_reverse state"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.options['palette_reverse']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get builin palettes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"_kql_raw_result_.palettes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get builtin palette by name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palettes['Oranges']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get builtin palette by index "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"_kql_raw_result_.palettes[5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get slice of an indexed builtin palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palettes[5][:6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get a color of a slice from an indexed builtin palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"_kql_raw_result_.palettes[5][:6][3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get slice of a named builtin palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_kql_raw_result_.palettes['terrain'][:6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get all the names of the builtin palettes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"list(_kql_raw_result_.palettes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get the raw rgb data of a slice from an indexed buitin palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"list(_kql_raw_result_.palettes[5][:6])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kql --help\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}