{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## PYT-DS: More Plotting in Matplotlib\n", "\n", "Lets keep revealing the mysteries of plotting with matplotlib. \n", "\n", "I'm still following VanderPlas closely as I think his materials are top notch. They're also freely available as open source products. Avail yourselves! Take advantage of what has been accomplished to make your lives easier!\n", "\n", "Links: \n", "\n", " * [HTML](https://jakevdp.github.io/PythonDataScienceHandbook/index.html) \n", " \n", " * [The Notebooks](https://github.com/jakevdp/PythonDataScienceHandbook/tree/master/notebooks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "plt.style.use('seaborn-white') # didn't have to import seaborn\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax_array = plt.subplots(2, 3, sharex='col', sharey='row') # ta da!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The axes show up in an array this time. We cycle through them..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# axes are in a two-dimensional array, indexed by [row, col]\n", "for i in range(2):\n", " for j in range(3):\n", " ax_array[i, j].text(0.5, 0.5, str((i, j)),\n", " fontsize=18, ha='center')\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.subplot(grid[0, 0])\n", "plt.subplot(grid[0, 1:])\n", "plt.subplot(grid[1, :2])\n", "plt.subplot(grid[1, 2]);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's lots going on in this VanderPlas plot below, a practical application of Gridspec. \n", "\n", "Gridspec gets the ball rollowing, at which point we create three subplots and name the axes.\n", "\n", "Notice the shared axes.\n", "\n", "numpy does the heavy lifting, in terms of providing 3000 points with a given mean and covariance. You might want to play around with these settings. Indeed, lets turn that into a....\n", "\n", "### LAB:\n", "\n", "What does the cov argument do? Try making some other changes, in terms of colors, marker size, transparency... Get familiar with the bells and whistles, starting with something that already works!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create some normally distributed data\n", "mean = [0, 0]\n", "cov = [[1, 1], [1, 2]]\n", "x, y = np.random.multivariate_normal(mean, cov, 3000).T\n", "\n", "# Set up the axes with gridspec\n", "fig = plt.figure(figsize=(6, 6))\n", "grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)\n", "main_ax = fig.add_subplot(grid[:-1, 1:])\n", "y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)\n", "x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)\n", "\n", "# scatter points on the main axes\n", "main_ax.plot(x, y, 'ok', markersize=3, alpha=0.2)\n", "\n", "# histogram on the attached axes\n", "x_hist.hist(x, 40, histtype='stepfilled',\n", " orientation='vertical', color='gray')\n", "x_hist.invert_yaxis()\n", "\n", "y_hist.hist(y, 40, histtype='stepfilled',\n", " orientation='horizontal', color='gray')\n", "y_hist.invert_xaxis()" ] } ], "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }