{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# matplotlib\n", "\n", "Matplotlib is the core plotting package in scientific python. There are others to explore as well (which we'll chat about on slack).\n", "\n", "Note: the latest version of matplotlib (2.0) introduced a number of style changes. This is the version we use here.\n", "\n", "Also, there are different interfaces for interacting with matplotlib, an interactive, function-driven (state machine) commandset and an object-oriented version. Usually for interactive work, we use the state interface." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want matplotlib to work inline in the notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib concepts\n", "\n", "Matplotlib was designed with the following goals (from mpl docs):\n", "\n", "* Plots should look great -- publication quality (e.g. antialiased)\n", "* Postscript output for inclusion with TeX documents\n", "* Embeddable in a graphical user interface for application development\n", "* Code should be easy to understand it and extend\n", "* Making plots should be easy\n", "\n", "Matplotlib is mostly for 2-d data, but there are some basic 3-d (surface) interfaces.\n", "\n", "Volumetric data requires a different approach" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Gallery\n", "\n", "Matplotlib has a great gallery on their webpage -- find something there close to what you are trying to do and use it as a starting point:\n", "\n", "http://matplotlib.org/gallery.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Importing\n", "\n", "There are several different interfaces for matplotlib (see http://matplotlib.org/faq/usage_faq.html)\n", "\n", "Basic ideas:\n", "\n", "* `matplotlib` is the entire package\n", "* `matplotlib.pyplot` is a module within matplotlib that provides easy access to the core plotting routines\n", "* `pylab` combines pyplot and numpy into a single namespace to give a MatLab like interface. You should avoid this—it might be removed in the future.\n", "\n", "There are a number of modules that extend its behavior, e.g. `basemap` for plotting on a sphere, `mplot3d` for 3-d surfaces\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Anatomy of a figure\n", "\n", "Figures are the highest level obect and can inlcude multiple axes\n", "![](anatomy1.png)\n", "\n", "(figure from: http://matplotlib.org/faq/usage_faq.html#parts-of-a-figure )\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Backends\n", "\n", "Interactive backends: pygtk, wxpython, tkinter, ...\n", "\n", "Hardcopy backends: PNG, PDF, PS, SVG, ...\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "plot() is the most basic command. Here we also see that we can use LaTeX notation for the axes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(0,2.0*np.pi, num=100)\n", "y = np.cos(x)\n", "\n", "plt.plot(x,y)\n", "plt.xlabel(r\"$x$\")\n", "plt.ylabel(r\"$\\cos(x)$\", fontsize=\"x-large\")\n", "plt.xlim(0, 2.0*np.pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that when we use the `plot()` command like this, matplotlib automatically creates a figure and an axis for us and it draws the plot on this for us. This is the _state machine_ interface. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "