{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook 4 - Basic Plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we will explore the basic plot interface using ``matplotlib.pyplot``. We will just scratch the surface of this vastly capable package: you can find out more about matplotlib [here](https://matplotlib.org/).\n", "\n", "This notebook a matplotlib version of [this web tutorial](http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html#)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A first plot: the matplotlib interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use matplotlib, we will need to import it. Since matplotlib is built on and designed to work with numpy, we should import this too." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make some simple data to plot: a sinusoid" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "x = np.arange(0, 20, 0.02) # 100 evenly-spaced values from 0 to 50\n", "y = np.sin(x)\n", "\n", "plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing the plot: Axes Limits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's play around with this a bit: first we can change the axis limits using ``xlim()`` and ``ylim()``" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y)\n", "plt.xlim(5, 15)\n", "plt.ylim(-1.2, 1.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing the plot: Axes Labels and Titles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can label the axes and add a title:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y)\n", "\n", "plt.xlabel('this is x!')\n", "plt.ylabel('this is y!')\n", "plt.title('My First Plot')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Labels can also be rendered using LaTeX symbols:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = np.sin(2 * np.pi * x)\n", "plt.plot(x, y)\n", "plt.title(r'$\\sin(2 \\pi x)$') # the `r` before the string indicates a \"raw string\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing the plot: Line Styles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can vary the line color or the line symbol:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y, '-r') # solid red line ('r' comes from RGB color scheme)\n", "plt.xlim(0, 10)\n", "plt.ylim(-1.2, 1.2)\n", "\n", "plt.xlabel('this is x!')\n", "plt.ylabel('this is y!')\n", "plt.title('My First Plot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other options for the color characters are:\n", "\n", " 'r' = red\n", " 'g' = green\n", " 'b' = blue\n", " 'c' = cyan\n", " 'm' = magenta\n", " 'y' = yellow\n", " 'k' = black\n", " 'w' = white\n", "\n", "Options for line styles are\n", "\n", " '-' = solid\n", " '--' = dashed\n", " ':' = dotted\n", " '-.' = dot-dashed\n", " '.' = points\n", " 'o' = filled circles\n", " '^' = filled triangles\n", "\n", "and many, many more.\n", "\n", "For more information, view the documentation of the plot function. In IPython, this can be\n", "accomplished using the ``?`` functionality:\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also see the online version of this help: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cusomizing the Plot: Legends" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple lines can be shown on the same plot. In this case, you can use a legend\n", "to label the two lines:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(0, 20, 0.02)\n", "y1 = np.sin(x)\n", "y2 = np.cos(x)\n", "\n", "plt.plot(x, y1, '-b', label='sine')\n", "plt.plot(x, y2, '-r', label='cosine')\n", "plt.legend(loc='upper right')\n", "plt.ylim(-1.5, 2.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise: Linestyles & Plot Customization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below are two sets of arrays ``x1, y1``, and ``x2, y2``. Create a plot where\n", "``x1`` and ``y1`` are represented by blue circles, and ``x2`` and ``y2`` are\n", "represented by a dotted black line. Label the symbols \"sampled\" and\n", "\"continuous\", and add a legend. Adjust the y limits to suit your taste." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x1 = np.arange(0, 10, 0.5)\n", "y1 = np.sin(x1)\n", "\n", "x2 = np.arange(0, 10, 0.01)\n", "y2 = np.sin(x2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 1 }