{ "cells": [ { "cell_type": "markdown", "id": "f9a9691c-c2c0-413f-a7e7-ab4e1140d9e3", "metadata": {}, "source": [ "# Bar Charts of Titanic Passenger Ages\n", "## Compute age frequencies with client-side Python" ] }, { "cell_type": "code", "execution_count": null, "id": "62f19ef0-cf1a-4b1b-8a5c-4af7cd3b46a7", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from DATA225utils import make_connection, dataframe_query" ] }, { "cell_type": "markdown", "id": "445ce3bc-c56a-4dc7-b1fa-95d4c2550d35", "metadata": {}, "source": [ "#### Download all the data and do the frequency calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "1361c45f-8522-4a27-ac02-72f5088af242", "metadata": { "tags": [] }, "outputs": [], "source": [ "conn = make_connection(config_file = 'titanic.ini')\n", "cursor = conn.cursor()" ] }, { "cell_type": "code", "execution_count": null, "id": "aba98674-937e-4a3a-85e6-45c1df14db39", "metadata": {}, "outputs": [], "source": [ "cursor.execute('SELECT * FROM passengers')\n", "rows = cursor.fetchall()" ] }, { "cell_type": "code", "execution_count": null, "id": "98be2e3c-c802-4a62-88bd-abcbaaeae78e", "metadata": {}, "outputs": [], "source": [ "baby = 0\n", "child = 0\n", "teen = 0\n", "youth = 0\n", "adult = 0\n", "senior = 0\n", "\n", "print(f'{rows[0] = }')\n", "print(f'{rows[0][3] = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "2c97c7b3-ec11-49ff-884f-3fa641942eeb", "metadata": {}, "outputs": [], "source": [ "for row in rows:\n", " age = row[3]\n", " \n", " if age != 0:\n", " age = float(age) # string to float\n", " \n", " if age >= 65: senior += 1\n", " elif age >= 25: adult += 1\n", " elif age >= 20: youth += 1\n", " elif age >= 13: teen += 1\n", " elif age >= 2: child += 1\n", " else: baby += 1" ] }, { "cell_type": "code", "execution_count": null, "id": "9b3a4f06-1fe0-4b93-a096-ced7f798ecf3", "metadata": {}, "outputs": [], "source": [ "print(f'{ baby = }')\n", "print(f'{ child = }')\n", "print(f'{ teen = }')\n", "print(f'{ youth = }')\n", "print(f'{ adult = }')\n", "print(f'{ senior = }')" ] }, { "cell_type": "markdown", "id": "e7d6a462-f180-4a66-a968-71dc107ac613", "metadata": { "tags": [] }, "source": [ "#### Create the bar chart." ] }, { "cell_type": "code", "execution_count": null, "id": "7dd6c047-c344-47ac-9955-7cff91e4a628", "metadata": {}, "outputs": [], "source": [ "labels = [ 'baby', 'child', 'teen', \n", " 'youth', 'adult', 'senior' ]\n", "counts = [ baby, child, teen,\n", " youth, adult, senior ]\n", "\n", "fig = plt.figure(figsize = (6, 5))\n", "\n", "plt.bar(labels, counts, color = 'maroon', width = 0.5)\n", "\n", "plt.xlabel('Age categories')\n", "plt.ylabel('Counts')\n", "plt.title('Titanic Age Counts')" ] }, { "cell_type": "code", "execution_count": null, "id": "d8df8b37-51b8-46f6-b1cb-78e869613c0e", "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "22527c19-2cb5-42e7-8dcf-d90daa83f1ac", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }