2024-heraklion-scientific-p.../code_snippets/walker_with_plotting.ipynb

106 lines
3.3 KiB
Plaintext
Raw Permalink Normal View History

2024-08-27 14:52:41 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2120045b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n",
" # ...\n",
"\n",
" def plot_trajectory(self, trajectory):\n",
" \"\"\" Plot a trajectory over a context map. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.matshow(self.context_map)\n",
" plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')\n",
" plt.show()\n",
"\n",
" def plot_trajectory_hexbin(self, trajectory):\n",
" \"\"\" Plot an hexagonal density map of a trajectory. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" with plt.rc_context({'figure.figsize': (4, 4), \n",
" 'axes.labelsize': 16, \n",
" 'xtick.labelsize': 14, \n",
" 'ytick.labelsize': 14}):\n",
" plt.hexbin(\n",
" trajectory[:, 1], trajectory[:, 0], \n",
" gridsize=30, extent=(0, 200, 0, 200), \n",
" edgecolors='none', cmap='Reds'\n",
" )\n",
" plt.gca().invert_yaxis()\n",
" plt.xlabel('X')\n",
" plt.ylabel('Y')\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8b1035c",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n",
" # ...\n",
"\n",
" def plot_trajectory(self, trajectory):\n",
" \"\"\" Plot a trajectory over a context map. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.matshow(self.context_map)\n",
" plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')\n",
" plt.show()\n",
"\n",
" def plot_trajectory_hexbin(self, trajectory):\n",
" \"\"\" Plot an hexagonal density map of a trajectory. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.hexbin(\n",
" trajectory[:, 1], trajectory[:, 0], \n",
" gridsize=30, extent=(0, 200, 0, 200), \n",
" edgecolors='none', cmap='Reds'\n",
" )\n",
" plt.gca().invert_yaxis()\n",
" plt.xlabel('X')\n",
" plt.ylabel('Y')\n",
" \n"
]
}
],
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}