Skip to content

Instantly share code, notes, and snippets.

@mthh
Created July 7, 2023 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mthh/c846dd04ae88a43b277f965808108bfd to your computer and use it in GitHub Desktop.
Save mthh/c846dd04ae88a43b277f965808108bfd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "618f2fe4-5dff-4679-bd7e-d888a40199f9",
"metadata": {},
"source": [
"# Prepare palettes for *dicopal.js*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d6722f3e-443d-4864-a4ca-7987d7864657",
"metadata": {},
"outputs": [],
"source": [
"import palettable\n",
"import re\n",
"import json\n",
"import os"
]
},
{
"cell_type": "markdown",
"id": "537d7691-796c-4f3a-9e3b-6db786d0dd40",
"metadata": {},
"source": [
"A list of colorblind-friendly palette (retrieved using a SPARQL query against a triplestore containing the [dicopal RDF vocabulary](https://lig-tdcge.imag.fr/steamer/dicopal/)). This list should be improved, as it does not cover all the palettes present `palettable` Python library."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f72e022e-3572-4d0d-ae7c-1a44b891db95",
"metadata": {},
"outputs": [],
"source": [
"cbf = ['RdPu_6',\n",
" 'Bilbao_7',\n",
" 'RdBu_5',\n",
" 'BuPu_7',\n",
" 'PiYG_6',\n",
" 'Bilbao_3',\n",
" 'YlGn_9',\n",
" 'Broc_4',\n",
" 'PuBu_3',\n",
" 'RdBu_3',\n",
" 'Set2_3',\n",
" 'PuBu_6',\n",
" 'Acton_8',\n",
" 'Acton_6',\n",
" 'BrBG_8',\n",
" 'Greys_5',\n",
" 'PiYG_10',\n",
" 'PuBuGn_6',\n",
" 'PuBuGn_7',\n",
" 'Purples_3',\n",
" 'Reds_6',\n",
" 'GnBu_3',\n",
" 'PRGn_7',\n",
" 'PiYG_8',\n",
" 'Broc_11',\n",
" 'PuOr_8',\n",
" 'Greys_8',\n",
" 'GnBu_9',\n",
" 'OrRd_8',\n",
" 'YlOrBr_7',\n",
" 'GnBu_6',\n",
" 'YlOrBr_3',\n",
" 'PiYG_7',\n",
" 'Blues_9',\n",
" 'Broc_10',\n",
" 'BrBG_9',\n",
" 'BuGn_4',\n",
" 'Okabe_Ito_Categorigal_8',\n",
" 'PuBu_4',\n",
" 'Blues_4',\n",
" 'PRGn_10',\n",
" 'RdYlBu_10',\n",
" 'Paired_3',\n",
" 'PuRd_4',\n",
" 'BrBG_7',\n",
" 'YlOrRd_8',\n",
" 'Greys_4',\n",
" 'RdPu_9',\n",
" 'YlGnBu_4',\n",
" 'Blues_7',\n",
" 'BrBG_4',\n",
" 'Greens_4',\n",
" 'RdYlBu_5',\n",
" 'Oranges_5',\n",
" 'Oranges_6',\n",
" 'Broc_7',\n",
" 'PRGn_8',\n",
" 'Purples_4',\n",
" 'OrRd_4',\n",
" 'YlGn_6',\n",
" 'RdBu_10',\n",
" 'YlGnBu_9',\n",
" 'Purples_6',\n",
" 'BuGn_8',\n",
" 'PuOr_9',\n",
" 'BuPu_5',\n",
" 'YlOrBr_4',\n",
" 'BuPu_9',\n",
" 'PiYG_5',\n",
" 'Greens_7',\n",
" 'Bilbao_4',\n",
" 'Bilbao_8',\n",
" 'PuBu_7',\n",
" 'YlGnBu_8',\n",
" 'Acton_9',\n",
" 'PuBuGn_3',\n",
" 'BuPu_8',\n",
" 'Greens_3',\n",
" 'Broc_9',\n",
" 'Oranges_8',\n",
" 'PuOr_3',\n",
" 'RdYlBu_7',\n",
" 'BrBG_6',\n",
" 'PiYG_4',\n",
" 'Broc_8',\n",
" 'PuRd_6',\n",
" 'PuBuGn_9',\n",
" 'Broc_3',\n",
" 'Greys_7',\n",
" 'RdYlBu_11',\n",
" 'BuGn_3',\n",
" 'Reds_3',\n",
" 'RdPu_8',\n",
" 'Oranges_9',\n",
" 'PRGn_11',\n",
" 'BuPu_4',\n",
" 'OrRd_7',\n",
" 'BuGn_5',\n",
" 'GnBu_5',\n",
" 'GnBu_8',\n",
" 'YlGnBu_7',\n",
" 'OrRd_6',\n",
" 'YlOrRd_3',\n",
" 'Reds_9',\n",
" 'PuOr_10',\n",
" 'BuPu_3',\n",
" 'RdBu_11',\n",
" 'RdBu_8',\n",
" 'PuOr_4',\n",
" 'Blues_8',\n",
" 'PRGn_9',\n",
" 'RdYlBu_3',\n",
" 'GreenMagenta_16',\n",
" 'PuRd_8',\n",
" 'Blues_3',\n",
" 'Broc_6',\n",
" 'Purples_5',\n",
" 'Purples_7',\n",
" 'Greys_3',\n",
" 'RdBu_9',\n",
" 'RdYlBu_9',\n",
" 'BuGn_6',\n",
" 'Greens_8',\n",
" 'PiYG_3',\n",
" 'Blues_6',\n",
" 'YlOrBr_8',\n",
" 'YlGn_7',\n",
" 'RdPu_3',\n",
" 'BuGn_9',\n",
" 'OrRd_3',\n",
" 'RdPu_7',\n",
" 'PRGn_4',\n",
" 'PuBuGn_4',\n",
" 'GnBu_4',\n",
" 'PRGn_5',\n",
" 'PuRd_5',\n",
" 'PuOr_7',\n",
" 'BrBG_11',\n",
" 'RdYlBu_6',\n",
" 'YlGnBu_5',\n",
" 'YlGn_5',\n",
" 'PuBu_8',\n",
" 'Greys_6',\n",
" 'Acton_3',\n",
" 'Acton_4',\n",
" 'BrBG_5',\n",
" 'Bilbao_5',\n",
" 'Bilbao_9',\n",
" 'Oranges_7',\n",
" 'Oranges_3',\n",
" 'RdBu_4',\n",
" 'Reds_5',\n",
" 'Reds_8',\n",
" 'RdYlBu_4',\n",
" 'PuBuGn_8',\n",
" 'PiYG_9',\n",
" 'BrBG_3',\n",
" 'PuOr_5',\n",
" 'Reds_7',\n",
" 'YlOrBr_5',\n",
" 'YlOrBr_9',\n",
" 'YlOrRd_4',\n",
" 'Greens_9',\n",
" 'YlGnBu_6',\n",
" 'Greens_6',\n",
" 'BuGn_7',\n",
" 'YlOrRd_5',\n",
" 'RdBu_7',\n",
" 'Reds_4',\n",
" 'PuRd_7',\n",
" 'PuBuGn_5',\n",
" 'Purples_9',\n",
" 'BrBG_10',\n",
" 'PRGn_6',\n",
" 'PuRd_3',\n",
" 'YlGn_3',\n",
" 'RdPu_4',\n",
" 'YlOrRd_7',\n",
" 'OrRd_5',\n",
" 'RdYlBu_8',\n",
" 'BuPu_6',\n",
" 'Dark2_3',\n",
" 'Acton_7',\n",
" 'PuRd_9',\n",
" 'Bilbao_6',\n",
" 'PiYG_11',\n",
" 'Oranges_4',\n",
" 'PuOr_11',\n",
" 'RdPu_5',\n",
" 'PuBu_5',\n",
" 'YlGnBu_3',\n",
" 'YlOrBr_6',\n",
" 'Purples_8',\n",
" 'Greens_5',\n",
" 'PRGn_3',\n",
" 'RdBu_6',\n",
" 'Paired_4',\n",
" 'YlGn_4',\n",
" 'YlGn_8',\n",
" 'PuBu_9',\n",
" 'YlOrRd_6',\n",
" 'Broc_5',\n",
" 'Greys_9',\n",
" 'Blues_5',\n",
" 'PuOr_6',\n",
" 'GnBu_7',\n",
" 'OrRd_9',\n",
" 'Acton_5',\n",
" 'Safe_2',\n",
" 'Safe_3',\n",
" 'Safe_4',\n",
" 'Safe_5',\n",
" 'Safe_6',\n",
" 'Safe_7',\n",
" 'Safe_8',\n",
" 'Safe_9',\n",
" 'Safe_10']"
]
},
{
"cell_type": "markdown",
"id": "38313133-bef3-4151-bbfe-c4b1ddebf596",
"metadata": {},
"source": [
"### Using the palettes from *palettable* Python library"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2496d324-4571-41b5-94b2-0195a4ac1307",
"metadata": {},
"outputs": [],
"source": [
"modules = [\n",
" ('cartocolors', 'diverging'),\n",
" ('cartocolors', 'sequential'),\n",
" ('cartocolors', 'qualitative'),\n",
" ('cmocean', 'diverging'),\n",
" ('cmocean', 'sequential'),\n",
" ('colorbrewer', 'diverging'),\n",
" ('colorbrewer', 'qualitative'),\n",
" ('colorbrewer', 'sequential'),\n",
" ('lightbartlein', 'diverging'),\n",
" ('lightbartlein', 'sequential'),\n",
" ('matplotlib', ''),\n",
" ('mycarta', ''),\n",
" ('scientific', 'diverging'),\n",
" ('scientific', 'sequential'),\n",
" ('tableau', ''),\n",
" ('wesanderson', ''),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6d502798-bbbe-4770-b637-0741134dcf5c",
"metadata": {},
"outputs": [],
"source": [
"res = {}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dba45f7f-78ab-4541-9f76-eda596b37c90",
"metadata": {},
"outputs": [],
"source": [
"for mod1, mod2 in modules:\n",
" if mod2 != '':\n",
" module = getattr(getattr(palettable, mod1), mod2)\n",
" else:\n",
" module = getattr(palettable, mod1)\n",
" \n",
" module_content = [e for e in dir(module) if re.search(\"_\\d\", e)]\n",
"\n",
" if res.get(mod1) is None:\n",
" res[mod1] = {}\n",
" \n",
" for pal_name in module_content:\n",
" pal = getattr(module, pal_name)\n",
"\n",
" # We dont store the reversed version of the palettes\n",
" is_reversed = False if not re.search(\"_r$\", pal.name) else True\n",
" if is_reversed: continue\n",
"\n",
" try:\n",
" short_name = pal.name[:re.search(\"_\\d(\\d)?$\", pal.name).start()]\n",
" except:\n",
" short_name = pal.name\n",
"\n",
" if res[mod1].get(short_name) is None:\n",
" res[mod1][short_name] = {}\n",
" res[mod1][short_name]['type'] = pal.type\n",
" res[mod1][short_name]['values'] = {}\n",
" \n",
" _id = f'{short_name}_{pal.number}'\n",
"\n",
" res[mod1][short_name]['values'][pal.number] = pal.hex_colors\n",
" \n",
" if hasattr(pal, 'url') and not res[mod1][short_name].get('url'):\n",
" res[mod1][short_name]['url'] = pal.url\n",
" elif mod1 == 'colorbrewer':\n",
" # Colorbrewer URL is missing in palettable palette descriptions\n",
" res[mod1][short_name]['url'] = 'https://colorbrewer2.org/'"
]
},
{
"cell_type": "markdown",
"id": "eb88f590-892b-4e01-b530-21ad4f9bd299",
"metadata": {},
"source": [
"Add the palette from Okabe & Ito (values were also retrieved using a SPARQL request from the dicopal RDF vocabulary)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0f5b11d8-ae65-48e6-a898-f501a04052f0",
"metadata": {},
"outputs": [],
"source": [
"res['otakeito'] = {\n",
" \"Okabe_Ito_Categorigal\": {\n",
" \"type\": \"qualitative\",\n",
" \"values\": { 8:\n",
" ['#000000',\n",
" '#e69f00',\n",
" '#56b4e9',\n",
" '#009e73',\n",
" '#f0e442',\n",
" '#0072b2',\n",
" '#d55e00',\n",
" '#cc79a7']\n",
" },\n",
" \"url\": \"https://jfly.uni-koeln.de/color/\",\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3956e2a3-edb1-475b-891f-419217258768",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.expanduser('~/code/dicopal.js/src/palettes.json'), 'w') as f:\n",
" f.write(json.dumps(res, indent=4))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8908d848-726d-488d-a896-c8ca790d41ca",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.expanduser('~/code/dicopal.js/src/cbf.json'), 'w') as f:\n",
" f.write(json.dumps(cbf))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.0rc1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment