{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scatter Plot with Marginal Histograms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This time, we will make use of `GridSpec` to visualize a scatter plot with marginal histograms." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: Animal mass vs. longevity" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('anage_data.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The given dataset is not complete. Filter the data so you end up with samples containing a body mass and a maximum longevity. Select all samples of the class Aves and with a body mass smaller than 20,000." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "longevity = 'Maximum longevity (yrs)'\n", "mass = 'Body mass (g)'\n", "\n", "# Remove records with missing values.\n", "data = data[ np.isfinite(data[longevity]) \n", " & np.isfinite(data[mass])\n", " ]\n", "\n", "# Sort according to class.\n", "aves = data[data['Class'] == 'Aves']\n", "aves = data[data[mass] < 20000]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create a figure with a constrained layout. Create a `GridSpec` object of size 4x4. Create a scatter plot of size 3x3 and marginal histograms of size 1x3 and 3x1. Add labels and a figure title." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the figure.\n", "fig = plt.figure(figsize=(8, 8), dpi=150, \n", " constrained_layout=True)\n", "\n", "# Create a GridSpec object.\n", "gs = fig.add_gridspec(4, 4)\n", "\n", "# Specify subplots.\n", "histx_ax = fig.add_subplot(gs[0, :-1])\n", "histy_ax = fig.add_subplot(gs[1:, -1])\n", "scatter_ax = fig.add_subplot(gs[1:, :-1])\n", " \n", "# Create the plots.\n", "scatter_ax.scatter(aves[mass], aves[longevity])\n", "histx_ax.hist(aves[mass], bins=20, density=True)\n", "histx_ax.set_xticks([])\n", "histy_ax.hist(aves[longevity], bins=20, \n", " density=True, orientation='horizontal')\n", "histy_ax.set_yticks([])\n", " \n", "# Add labels.\n", "plt.xlabel('Body mass in grams')\n", "plt.ylabel('Maximum longevity in years')\n", " \n", "# Add the super title.\n", "plt.suptitle('Scatter plot with marginal histograms')\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" ] }, { "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 }