Created
October 21, 2024 00:57
-
-
Save ajfriend/40b0d5a2b8d7ea02fa8f35574aab65b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "643e8564-5d5f-4f09-bd0a-334e6b122c3f", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:16.915105Z", | |
"iopub.status.busy": "2024-10-21T00:56:16.914886Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.275566Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.275275Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:16.915086Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import matplotlib\n", | |
"import palettable\n", | |
"from lonboard.colormap import apply_continuous_cmap\n", | |
"\n", | |
"cmap = palettable.colorbrewer.sequential.YlOrRd_9\n", | |
"# cmap = matplotlib.colormaps['magma']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "77fd41ec-9854-4c2e-9b3a-da5e908af4a1", | |
"metadata": {}, | |
"source": [ | |
"# `apply_continuous_cmap` mutates the column it operates on\n", | |
"\n", | |
"- this happens for both `palettable` and `matplotlib` colormaps" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "ff6dad62-0170-4086-95a9-0477efe025e1", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.275957Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.275862Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.280191Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.280005Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.275951Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>name</th>\n", | |
" <th>value</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>a</td>\n", | |
" <td>0.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>b</td>\n", | |
" <td>0.2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>c</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>d</td>\n", | |
" <td>0.4</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" name value\n", | |
"0 a 0.1\n", | |
"1 b 0.2\n", | |
"2 c 0.3\n", | |
"3 d 0.4" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame({'name': ['a', 'b', 'c', 'd'], 'value': [.1, .2, .3, .4]})\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "c7fa9f84-a508-49c5-8dea-dc1115297291", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.280598Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.280503Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.282779Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.282586Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.280591Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[255, 240, 169],\n", | |
" [254, 225, 134],\n", | |
" [254, 202, 101],\n", | |
" [253, 170, 72]], dtype=uint8)" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_continuous_cmap(df['value'], cmap)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "80334cad-f621-471d-ab3d-a181d38438a3", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.283121Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.283064Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.285371Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.285203Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.283115Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>name</th>\n", | |
" <th>value</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>a</td>\n", | |
" <td>25.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>b</td>\n", | |
" <td>51.2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>c</td>\n", | |
" <td>76.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>d</td>\n", | |
" <td>102.4</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" name value\n", | |
"0 a 25.6\n", | |
"1 b 51.2\n", | |
"2 c 76.8\n", | |
"3 d 102.4" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# value column is changed!\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "56552964-e78f-47d3-9c62-cf13abedd3e8", | |
"metadata": {}, | |
"source": [ | |
"# Curiously, it doesn't do the same to a numpy array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "ceaabc5b-c484-4181-a1e2-ed626bb61f32", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.285685Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.285630Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.287116Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.286913Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.285678Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"value = np.array([.1, .2, .3, .4])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "95113463-ec06-488a-bb3b-af7474073d2a", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.288215Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.288091Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.290105Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.289947Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.288208Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[255, 240, 169],\n", | |
" [254, 225, 134],\n", | |
" [254, 202, 101],\n", | |
" [253, 170, 72]], dtype=uint8)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_continuous_cmap(value, cmap)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "b0a73f3f-552f-4624-9dff-d0f5ea242f03", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.290339Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.290289Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.291895Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.291744Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.290333Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0.1, 0.2, 0.3, 0.4])" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# value array stays the same :)\n", | |
"value" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4b8a83a8-60fa-464c-a668-32474640f367", | |
"metadata": {}, | |
"source": [ | |
"# It also mutates a regular pandas Series" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "f14111fc-1c0f-40ec-b84c-bee12330f542", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.292194Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.292084Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.293632Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.293482Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.292185Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"value = pd.Series([.1, .2, .3, .4])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "84535138-36fb-41bc-95a7-bbf0ccc15830", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.293997Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.293912Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.295988Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.295777Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.293991Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[255, 240, 169],\n", | |
" [254, 225, 134],\n", | |
" [254, 202, 101],\n", | |
" [253, 170, 72]], dtype=uint8)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"apply_continuous_cmap(value, cmap)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "ace08e33-588a-4f97-b42d-6795b12089fd", | |
"metadata": { | |
"execution": { | |
"iopub.execute_input": "2024-10-21T00:56:17.296279Z", | |
"iopub.status.busy": "2024-10-21T00:56:17.296224Z", | |
"iopub.status.idle": "2024-10-21T00:56:17.299273Z", | |
"shell.execute_reply": "2024-10-21T00:56:17.298771Z", | |
"shell.execute_reply.started": "2024-10-21T00:56:17.296273Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 25.6\n", | |
"1 51.2\n", | |
"2 76.8\n", | |
"3 102.4\n", | |
"dtype: float64" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# value Series is changed!\n", | |
"value" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "fb72b3c0-7b1b-49b5-bd44-2893121ce36c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment