{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffc1e1d9-fa74-4121-aa87-e1a8665e4c2b",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme()"
   ]
  },
  {
   "cell_type": "raw",
   "id": "41b4f602-32af-44f8-bf1a-0f1695c9abbb",
   "metadata": {},
   "source": [
    "Plot a heatmap with row and column clustering:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c715bd8f-cf5d-4caa-9244-336b3d0248a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = sns.load_dataset(\"iris\")\n",
    "species = iris.pop(\"species\")\n",
    "sns.clustermap(iris)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "1cc3134c-579a-442a-97d8-a878651ce90a",
   "metadata": {},
   "source": [
    "Change the size and layout of the figure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd33cf4b-9589-4b9a-a246-0b95bad28c51",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.clustermap(\n",
    "    iris,\n",
    "    figsize=(7, 5),\n",
    "    row_cluster=False,\n",
    "    dendrogram_ratio=(.1, .2),\n",
    "    cbar_pos=(0, .2, .03, .4)\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c5d3408d-f5d6-4045-9d61-15573a981587",
   "metadata": {},
   "source": [
    "Add colored labels to identify observations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79d3fe52-6146-4f33-a39a-1d4a47243ea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "lut = dict(zip(species.unique(), \"rbg\"))\n",
    "row_colors = species.map(lut)\n",
    "sns.clustermap(iris, row_colors=row_colors)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f2f944e2-36cd-4653-86b4-6d2affec13d6",
   "metadata": {},
   "source": [
    "Use a different colormap and adjust the limits of the color range:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6137c7ad-db92-47b8-9d00-3228c4e1f7df",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.clustermap(iris, cmap=\"mako\", vmin=0, vmax=10)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "93f96d1c-9d04-464f-93c9-4319caa8504a",
   "metadata": {},
   "source": [
    "Use differente clustering parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9e76bde-a222-4eca-971f-54f56ad53281",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.clustermap(iris, metric=\"correlation\", method=\"single\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "ea6ed3fd-188d-4244-adac-ec0169c02205",
   "metadata": {},
   "source": [
    "Standardize the data within the columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5f744c4-b959-4ed1-b2cf-6046c9214568",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.clustermap(iris, standard_scale=1)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7ca72242-4eb0-4f8e-b0c0-d1ef7166b738",
   "metadata": {},
   "source": [
    "Normalize the data within rows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33815c4c-9bae-4226-bd11-3dfdb7ecab2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.clustermap(iris, z_score=0, cmap=\"vlag\", center=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f37d57a-b049-4665-9c24-4d5fbbca00ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "py310"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
