{ "cells": [ { "cell_type": "markdown", "id": "04e0612f-4319-4261-ad7e-61fd03e8dc67", "metadata": {}, "source": [ "# Plot track density" ] }, { "cell_type": "code", "execution_count": null, "id": "8ab3d052-d18b-4452-8582-638cd69860b4", "metadata": {}, "outputs": [], "source": [ "import huracanpy\n", "\n", "import cartopy.crs as ccrs" ] }, { "cell_type": "markdown", "id": "261ee449-f5e8-442b-994c-73d608829204", "metadata": {}, "source": [ "## Basic routine" ] }, { "cell_type": "code", "execution_count": null, "id": "a5c041bf-cf40-44d6-97ba-6cce60e42e9c", "metadata": {}, "outputs": [], "source": [ "# Load data (example : ibtracs)\n", "tracks = huracanpy.load(source=\"ibtracs\")" ] }, { "cell_type": "markdown", "id": "026ef63e-9ece-4d2b-99f9-1c578c94cf7f", "metadata": {}, "source": [ "To plot the track density, you need two functions. The first one, `huracanpy.diags.track_density.simple_global_histogram` computes the track density, which is stored in a 2D xarray. The second one `huracanpy.plot.density.plot_density` will plot the track density. Because the track density is a xarray object, you can also use built-in xarray functions." ] }, { "cell_type": "code", "execution_count": null, "id": "3edeabba-475a-4b9b-a937-bd196cb2b40a", "metadata": {}, "outputs": [], "source": [ "# Compute track density\n", "D = huracanpy.calc.density(tracks.lon, tracks.lat)\n", "D # D, the track density, is a map stored in xarray" ] }, { "cell_type": "code", "execution_count": null, "id": "8c65a7fe-8672-4903-bbbc-b39aafb45243", "metadata": {}, "outputs": [], "source": [ "# Plotting using huracanpy function\n", "huracanpy.plot.density(D)" ] }, { "cell_type": "code", "execution_count": null, "id": "3e11e8de-5bb0-4997-9743-fcc3fe40a372", "metadata": {}, "outputs": [], "source": [ "# Plotting using xarray's plot function\n", "D.plot()" ] }, { "cell_type": "markdown", "id": "c7d322fe-9d22-4147-a403-26373e2e7336", "metadata": {}, "source": [ "## Customization" ] }, { "cell_type": "markdown", "id": "a3c035e4-69b2-40cc-a03a-8a25ea12c082", "metadata": {}, "source": [ "### Density computation" ] }, { "cell_type": "markdown", "id": "9d565d25-8c48-4321-a36b-fea62ece433b", "metadata": {}, "source": [ "For the track density computation, there are two things that can be customized : \n", "* `bin_size` : The size of the boxes over which the track density is computed, in degrees.\n", "* `N_seasons` is a normalization factor. It is useful to make the number make sense, such as \"number of TC point per year in a `bin_size x bin_size` box\"" ] }, { "cell_type": "code", "execution_count": null, "id": "1271d02c-59b9-414e-b229-8844408ecce0", "metadata": {}, "outputs": [], "source": [ "# Setting a smaller bin_size\n", "D = huracanpy.calc.density(tracks.lon, tracks.lat, bin_size=1)\n", "D.plot()\n", "D" ] }, { "cell_type": "code", "execution_count": null, "id": "abb6c39b-100a-4f95-be9b-868c56564df5", "metadata": {}, "outputs": [], "source": [ "# Setting a larger bin_size\n", "D = huracanpy.calc.density(tracks.lon, tracks.lat, bin_size=10)\n", "D.plot()\n", "D" ] }, { "cell_type": "code", "execution_count": null, "id": "50aec700-e567-4c9e-9fbd-c967a7244222", "metadata": {}, "outputs": [], "source": [ "# Normalizing\n", "## Computing the number of season in the dataset\n", "import numpy as np\n", "\n", "N = len(np.unique(tracks.season.values))\n", "print(N)\n", "## Track density with normalization.\n", "D = huracanpy.calc.density(tracks.lon, tracks.lat, n_seasons=N)\n", "# In this case, now the number in D in number of TC points in each 5x5 box.\n", "D.plot()" ] }, { "cell_type": "markdown", "id": "1a200209-83ec-45a7-864e-8da1444cacfa", "metadata": {}, "source": [ "### Plotting" ] }, { "cell_type": "code", "execution_count": null, "id": "64adaac9-dc53-4d56-8791-37a6cc9fcc4f", "metadata": {}, "outputs": [], "source": [ "# With huracanpy's function\n", "## The function is based on matplotlib's contourf, so you can use its options\n", "huracanpy.plot.density(D, contourf_kws=dict(cmap=\"tab20c_r\", levels=20))\n", "## Changing the projection with subplot_kws\n", "huracanpy.plot.density(D, subplot_kws=dict(projection=ccrs.Mollweide(180)))\n", "## Changing the figure's properties\n", "huracanpy.plot.density(D, fig_kws=dict(figsize=(15, 5)))" ] } ], "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }