{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "abb59a0a-3ee2-4f4e-9661-0b2aceff89d4",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Plotting examples\n",
    "\n",
    "## Stacked plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3dc7a7a4-1176-4117-821d-4fc5a9003b45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 187940 characters]\n",
      "text/plain": [
       "<Figure size 400x600 with 21 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib.pyplot import cm\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import lognorm\n",
    "from matplotlib import rcParams\n",
    "from aerosol.plotting import stacked_plots,rotate_xticks\n",
    "\n",
    "rcParams.update({'font.size': 12, \n",
    "                 \"font.family\": \"times new roman\",\n",
    "                 \"mathtext.fontset\" : \"stix\"\n",
    "                })\n",
    "\n",
    "# Generate data\n",
    "s = np.linspace(0.5,1.5,20)\n",
    "x = np.linspace(0,3.0,10000)\n",
    "ylist=[]\n",
    "ylabel=[]\n",
    "for i in range(20):\n",
    "    ylist.append(lognorm.pdf(x, s[i]))\n",
    "    ylabel.append(\"$s = %.2f$\"% s[i])\n",
    "df=pd.DataFrame(\n",
    "    columns = ylabel, \n",
    "    index = x, \n",
    "    data = np.stack(ylist).T )\n",
    "\n",
    "# Plot \n",
    "fig,axx,axy = stacked_plots(\n",
    "    df,\n",
    "    height_coef=5,\n",
    "    spacing_coef=0.8,\n",
    "    plot_type=\"fill_between\",\n",
    "    cmap=cm.magma_r,\n",
    "    edgecolor='black',\n",
    "    linewidth=1)\n",
    "\n",
    "rotate_xticks(axx,30)\n",
    "axx.set_xlabel(\"$x$\")\n",
    "axy.set_ylabel(\"$f(x)$\")\n",
    "fig.set_figheight(6)\n",
    "fig.set_figwidth(4)\n",
    "fig.suptitle(\"Log-normal distributions\\n $f(x;s) = \\\n",
    "    \\\\frac{1}{sx\\\\sqrt{2\\\\pi}}e^{-\\\\frac{\\\\log^2 x}{2s^2}}$\",\n",
    "    y=1.05,fontsize=12)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22c156fa-d246-4ae8-93da-8c0ba536490d",
   "metadata": {},
   "source": [
    "## Aerosol size distribution subplots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bf88334a-950d-43de-bde8-fac6e7bf0306",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 141688 characters]\n",
      "text/plain": [
       "<Figure size 600x600 with 17 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import xarray as xr\n",
    "from matplotlib.pyplot import cm\n",
    "from matplotlib import colors\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import lognorm\n",
    "from matplotlib import rcParams\n",
    "from aerosol.plotting import subplot_aerosol_dist\n",
    "\n",
    "rcParams.update({'font.size': 12, \n",
    "                 \"font.family\": \"times new roman\",\n",
    "                 \"mathtext.fontset\" : \"stix\"\n",
    "                })\n",
    "\n",
    "# Load data\n",
    "ds = xr.open_dataset(\"NAIS_20220209.nc\")\n",
    "ds.close()\n",
    "v = [ds.pos_ions.to_pandas()]*15\n",
    "\n",
    "# Plot\n",
    "fig,ax,cax = subplot_aerosol_dist(\n",
    "    v,\n",
    "    (4,4),\n",
    "    cmap=cm.turbo,\n",
    "    norm=colors.SymLogNorm(1,vmin=10,vmax=10000),\n",
    "    xminortick_interval=\"3H\",\n",
    "    xmajortick_interval=\"9H\",\n",
    "    xticklabel_format=\"%H:%M\",\n",
    "    keep_inner_ticklabels = False,\n",
    "    label_subplots = True,\n",
    "    label_color = \"black\"\n",
    ")\n",
    "\n",
    "# Add y-axis label\n",
    "fig.text(0.02, 0.5, '$D_p$, [m]', ha='center', va='center', rotation='vertical')\n",
    "cax.set_label(\"$dN/d\\\\log D_p$, [cm$^{-3}$]\")\n",
    "fig.set_figheight(6)\n",
    "fig.set_figwidth(6)\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "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.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
