{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Analysis with Jupyter Notebooks.\n", "\n", "# Tutorial 4\n", "\n", "Benjamin J. Morgan, University of Bath." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Contents\n", "\n", "- [Plotting data with matplotlib](#matplotlib)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting data with matplotlib\n", "\n", "To plot data we use another module: [`matplotlib`](http://matplotlib.org) This is a very powerful (and complicated) plotting library, that be used for quick analysis of experimental data, or to generate publication quality figures. It supports an enormous number of plot types. We are going to start with simple 2D $x,y$ plots.\n", "\n", ">```python\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import numpy as np\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `import` statement loads up the part of the `matplotlib` library we will use for plotting, and lets us refer to this as `plt` for convenience later.\n", "\n", "The `%matplotlib inline` command tells the Jupyter notebook that we want all out “plots” to appear “inline”, i.e. inside the notebook (alternatives include opening the plots in other windows, or saving them as graphics files). The `%` symbol at the start means this is a “magic” command for controlling the behaviour of this Jupyter notebook, and is not standard Python.\n", "\n", "If you are using a high resolution screen, you will also want to switch on high resolution figures.\n", "\n", ">```python\n", "%config InlineBackend.figure_format = 'retina'\n", "```\n", "\n", "We also import `numpy` as `np` so that we can store our data as arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creating a plot uses `plt.plot()`. Remember, we have assigned `plt` as shorthand for `matplotlib.pyplot`.\n", "\n", ">```python\n", "# plot the numpy arrays a and b against each other\n", "import numpy as np\n", "a = np.array( [ 1, 2, 3, 4 ] )\n", "b = np.array( [ 5, 6, 7, 8 ] )\n", "print( \"a:\", a )\n", "print( \"b:\", b )\n", "plt.plot( a, b )\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be used for plotting $y$ as a function of $x$, e.g. $y=x^2$.\n", "\n", ">```python\n", "x = np.array( [0, 1, 2, 3, 4, 5] )\n", "y = x**2\n", "plt.plot( x, y )\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Plot $y=\\sin(x)$ for $x=0$ to $2\\pi$.
\n", "\n", "To generate an array from 0 to $2\\pi$ remember you can use numpy.linspace()\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default plot shows a connected line. To plot individual points, we can add a third argument to `plt.plot()` that specifies the appearance for that data set:\n", "\n", ">```python\n", "plt.plot( x, y, \"o\" )\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A large number of marker types exist in `matplotlib` (a full list is [here](#http://matplotlib.org/api/markers_api.html#module-matplotlib.markers))." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also control the line style, and combine code controlling marker and line appearance.\n", "\n", ">```python\n", "plt.plot( x, y, \":\" ) # dotted line\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "plt.plot( x, y, \"s:\" ) # dotted line with squares\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding axes labels and a title uses the `xlabel()`, `ylabel()`, and `title` commands." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the previous cell to include the following lines between plt.plot() and plt.show():

\n", "\n", "\n", "plt.xlabel( 'x' )
\n", "plt.ylabel( 'y^2' )
\n", "plt.title( 'y = x^2' )
\n", "

\n", "\n", "Rerun the code cell to regenerate the plot, now with labelled axes and a title.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting multiple data sets on the same graph uses multiple `plot()` commands. For an example, let us create three `numpy` arrays, `u`, `v`, and `w`.\n", "\n", ">```python\n", "# create three numpy arrays, u, v, and w\n", "u = x + 1\n", "v = x ** 2\n", "w = np.sqrt( (x*2)+1 )\n", "print('u = ',u)\n", "print('v = ',v)\n", "print('w = ',w)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can plot $u$, $v$, and $w$ versus $x$ on the same figure.\n", "\n", ">```python\n", "plt.plot( x, u, 'o-', label='x+1' )\n", "plt.plot( x, v, 'x--', label='x**2' )\n", "plt.plot( x, w, '*:', label='sqrt((x*2)+1)' )\n", "plt.xlabel( 'x' )\n", "plt.ylabel( 'y' )\n", "plt.title( 'y=f(x)')\n", "plt.legend()\n", "plt.show()\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have assigned text labels for each data set by setting `label=string` in each `plt.plot()` command. These labels are then shown in the legend produced by the `plt.legend()` command." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Plot $sin(x)$ and $cos(x)$ for $x=0$ to $2\\pi$. Label the curves and the $x$ axis.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nearly every part of the plot appearance can be controlled. Two further examples are line colours and thickness. A number of line colours are predefined and can be referred to with a [corresponding string](http://matplotlib.org/examples/color/named_colors.html)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# run this cell\n", "plt.plot( x, u, 'o-', label='x+1', color='salmon', linewidth=3 )\n", "plt.plot( x, v, 'x--', label='x**2', color='darkolivegreen', linewidth=2 )\n", "plt.plot( x, w, '*:', label='sqrt((x*2)+1)', color='slategrey', linewidth=4 )\n", "plt.xlabel( 'x' )\n", "plt.ylabel( 'y' )\n", "plt.title( 'Too many options can lead to ugly graphs' )\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can save a figure to an external file using `plt.savefig('filename')` instead of `plt.show()`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the cell above to replace

plt.show()

with

plt.savefig('my_figure.pdf')

Then run the cell to save the figure to the disk.\n", "
" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }