{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `seaborn` Tree maps and the `squarify` module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import squarify\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A ***tree map*** displays hiearchical data as a set of nested rectangles. Each rectangle represents a group, and its area is proportional to its value. Colors can represent the hierarchy. Use the `squarify` module with color palettes from `seaborn`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors = sns.light_palette('brown', 4)\n", "\n", "squarify.plot(sizes=[50, 25, 10, 15],\n", " label=['Group A', 'Group B', 'Group C', 'Group D'],\n", " color=colors)\n", "\n", "plt.axis('off')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Water usage\n", "#### Use a tree map to visualize water usage." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "usage = pd.read_csv(\"water_usage.csv\")\n", "usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Show the percentages for each tile and add a title." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(dpi=200)\n", "\n", "labels = usage['Usage'] + ' (' + usage['Percentage'].astype('str') + '%)'\n", "\n", "squarify.plot(sizes=usage['Percentage'], \n", " label=labels, \n", " color=sns.light_palette('green', usage.shape[0]))\n", "\n", "plt.axis('off')\n", "plt.title('Water usage')\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adapted from ***Data Visualization with Python***, by Mario Döbler and Tim Großmann, Packt 2019, ISBN 978-1-78995-646-7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (c) 2024 by Ronald Mak" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }