data/notebooks/Lab/color_dict.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2023-12-20T12:01:24.517389896Z",
"start_time": "2023-12-20T12:01:24.515214956Z"
}
},
"outputs": [],
"source": [
"import json\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from matplotlib.colors import rgb2hex\n",
"\n",
"\n",
"def generate_colormap_dict(cmap_name, n, vmin, vmax):\n",
" cmap = plt.get_cmap(cmap_name)\n",
" colors = cmap(np.linspace(0, 1, n))\n",
" values = np.linspace(vmin, vmax, n)\n",
" color_dict = [\n",
" {\"color\": rgb2hex(color), \"value\": str(value)} for color, value in zip(colors, values)\n",
" ]\n",
" return json.dumps(color_dict)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "'[{\"color\": \"#ffffcc\", \"value\": \"0.0\"}, {\"color\": \"#fed976\", \"value\": \"0.000625\"}, {\"color\": \"#fd8c3c\", \"value\": \"0.00125\"}, {\"color\": \"#e2191c\", \"value\": \"0.001875\"}, {\"color\": \"#800026\", \"value\": \"0.0025\"}]'"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_colormap_dict(\"YlOrRd\", 5, 0, 0.0025)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-20T12:01:25.158357764Z",
"start_time": "2023-12-20T12:01:25.154326681Z"
}
},
"id": "15ad6dbc99a8ffcc"
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [
{
"data": {
"text/plain": "'[{\"color\": \"#f7fbff\", \"value\": \"0.0\"}, {\"color\": \"#c6dbef\", \"value\": \"0.0025\"}, {\"color\": \"#6aaed6\", \"value\": \"0.005\"}, {\"color\": \"#2070b4\", \"value\": \"0.0075\"}, {\"color\": \"#08306b\", \"value\": \"0.01\"}]'"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_colormap_dict(\"Blues\", 5, 0, 0.01)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-20T12:04:21.700652510Z",
"start_time": "2023-12-20T12:04:21.659523456Z"
}
},
"id": "64268fefa8441e98"
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [
{
"data": {
"text/plain": "'[{\"color\": \"#ffffe5\", \"value\": \"0.0\"}, {\"color\": \"#d9f0a3\", \"value\": \"0.0025\"}, {\"color\": \"#77c679\", \"value\": \"0.005\"}, {\"color\": \"#228343\", \"value\": \"0.0075\"}, {\"color\": \"#004529\", \"value\": \"0.01\"}]'"
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_colormap_dict(\"YlGn\", 5, 0, 0.01)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-20T12:03:20.695073782Z",
"start_time": "2023-12-20T12:03:20.640846083Z"
}
},
"id": "b92d34b1b8a350c9"
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"data": {
"text/plain": "'[{\"color\": \"#fff7f3\", \"value\": \"0.0\"}, {\"color\": \"#fcc5c0\", \"value\": \"0.0025\"}, {\"color\": \"#f767a1\", \"value\": \"0.005\"}, {\"color\": \"#ad017e\", \"value\": \"0.0075\"}, {\"color\": \"#49006a\", \"value\": \"0.01\"}]'"
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_colormap_dict(\"RdPu\", 5, 0, 0.01)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-20T12:03:52.962917750Z",
"start_time": "2023-12-20T12:03:52.942415581Z"
}
},
"id": "2d7fc6eb3c0d2f2f"
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "6bd0f54aefd49f67"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}