{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Table Demo\n\nDemo of table function to display a table within a plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\ndata = [[ 66386, 174296, 75131, 577908, 32015],\n [ 58230, 381139, 78045, 99308, 160454],\n [ 89135, 80552, 152558, 497981, 603535],\n [ 78415, 81858, 150656, 193263, 69638],\n [139361, 331509, 343164, 781380, 52269]]\n\ncolumns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')\nrows = ['%d year' % x for x in (100, 50, 20, 10, 5)]\n\nvalues = np.arange(0, 2500, 500)\nvalue_increment = 1000\n\n# Get some pastel shades for the colors\ncolors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))\nn_rows = len(data)\n\nindex = np.arange(len(columns)) + 0.3\nbar_width = 0.4\n\n# Initialize the vertical-offset for the stacked bar chart.\ny_offset = np.zeros(len(columns))\n\n# Plot bars and create text labels for the table\ncell_text = []\nfor row in range(n_rows):\n plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])\n y_offset = y_offset + data[row]\n cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])\n# Reverse colors and text labels to display the last value at the top.\ncolors = colors[::-1]\ncell_text.reverse()\n\n# Add a table at the bottom of the Axes\nthe_table = plt.table(cellText=cell_text,\n rowLabels=rows,\n rowColours=colors,\n colLabels=columns,\n loc='bottom')\n\n# Adjust layout to make room for the table:\nplt.subplots_adjust(left=0.2, bottom=0.2)\n\nplt.ylabel(f\"Loss in ${value_increment}'s\")\nplt.yticks(values * value_increment, ['%d' % val for val in values])\nplt.xticks([])\nplt.title('Loss by Disaster')\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }