{ "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": [ "# Pie Charts: Water Usage" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pie chart parameters\n", "#### To create a pie chart, call `plt.pie(x, explode, labels, autopct)`, where\n", "- #### `x` is a sequence of the proportions of the slice sizes\n", "- #### `explode` is a sequence of the fractions of the radius offets for the slices (optional)\n", "- #### `labels` is the sequence of labels for the slices (optional)\n", "- #### `autopct` shows percentages inside the slices according for the format string (optinal)\n", "#### Example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.pie([0.4, 0.3, 0.2, 0.1],\n", " explode=[0.1, 0, 0, 0],\n", " labels=['Dept. A', 'Dept. B', 'Dept. C', 'Dept. D'])\n", "\n", "plt.title('Departmental Shares')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use a pie chart to visualize the water usage. Highlight one usage of your choice using the explode parameter. Show the percentages for each slice and add a title." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('water_usage.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the figure.\n", "plt.figure(figsize=(8, 8), dpi=300)\n", "\n", "# Create the pie chart.\n", "plt.pie('Percentage', explode=(0, 0, 0.1, 0, 0, 0), \n", " labels='Usage', data=data, autopct='%.0f%%')\n", "\n", "plt.title('Water usage')\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }