{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

Introduction to Plotting in Python

\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Contents

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "

^ Imports

\n", "

\n", "To make plots in Python, we'll need to import some things from two\n", "different python libraries matplotlib and numpy\n", "\n", "

matplotlib

\n", "\n", "Matplotlib is used to plot data, and make various kinds of graphics and\n", "animations in python. There are other graphics and plotting libraries,\n", "but matplotlib is probably the most commonly used. The matplotlib\n", "interface is not (in my opinion) either easy,\n", "intuitive, or \"logical.\" But we're using it because (1) it's powerful,\n", "and (2) it's sort of \"the standard\" (as least for now).\n", "

\n", "My strategy for dealing with matplotlib is to keep a bunch of\n", "examples handy, so I can look at them when I want to plot something.\n", "That's basically what I hope to provide for you in this notebook.\n", "\n", "

numpy

\n", "\n", "Numpy stands for \"numeric python.\" It is useful in working with data to\n", "get it into a form that you can plot. Numpy basically deals with \n", "lists of numbers. I'll explain this further as we go." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The following line keeps the notebook from saving changes\n", "# automatically. If you make changes that you want to save,\n", "# select File -> Save and Checkpoint, from the menus at the top.\n", "\n", "%autosave 0\n", "\n", "# Next line is just something that you need if you are going to\n", "# use matplotlib to make diagrams in a noteook.\n", "\n", "%matplotlib inline\n", "\n", "# The main thing we need from matplotlib, to get started, is an\n", "# object called \"pyplot.\" When we import it, we'll rename it just \"plt\"\n", "# (simply so that we have a shorter thing to type).\n", "\n", "from matplotlib import pyplot as plt\n", "\n", "# From numpy we'll import a bunch of mathematical functions\n", "\n", "from numpy import sin, cos, exp, sqrt\n", "\n", "# And, we'll import something called \"linspace.\" This is used to\n", "# create what you might think of as the \"x axis\" for a plot. But\n", "# we'll get into that down below.\n", "\n", "from numpy import linspace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "

^ Points

\n", "

\n", "In this notebook we're going to stick to drawing diagrams in\n", "two dimensions. In 2D a \"point\" is simply a list of two\n", "numbers (for example the x and y coordinates).\n", "

\n", "In your program, there are different ways you might specify a point.\n", "You might explicity name both coordinates:\n", "
\n", "\n", "x_coord = 2.6\n", "y_coord = 3.2\n", "\n", "\n", "Or you might make one object which holds both coordinates:\n", "
\n", "\n", "p = [2.6, 3.2]\n", "\n", "\n", "We use square brackets to make a list of numbers.\n", "We can\n", "then refer to one of the numbers by \"indexing\"\n", "into the list with square brackets, like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define a point\n", "p = [2.6, 3.2]\n", "# Print the point\n", "print(p)\n", "# Print just the first coordinate\n", "print(p[0])\n", "# Print just the second coordinate\n", "print(p[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Here's how to do \"better\" with the print statement. You use\n", "# %s where you want the variable to go, and then you put the\n", "# variable itself after another % at the end ...\n", "p = [2.6, 3.2]\n", "print('The point is %s'%p)\n", "print('The x coordinate is %s'%p[0])\n", "print('The y coordinate is %s'%p[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Now we'll plot the point

\n", "\n", "The \"plt\" object that we imported from matplotlib has a method\n", "(a function) called \"plot\" that is used for making various kinds\n", "of plots. To plot a single point, we give it the x and y coordinates\n", "and a parameter called \"marker\" (which, for now we'll just use the\n", "letter 'o').\n", "\n", "The semicolon at the end of the plot statement just keeps it from\n", "echoing out some extra stuff. You can try deleting the semicolon\n", "to see what happens." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_coord = 2.6\n", "y_coord = 3.2\n", "plt.plot(x_coord, y_coord, marker='o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to use the point \"p\" that we already defined as a list\n", "of two numbers, then we have to index it to give the individual\n", "numbers to the plot function. (So in this particular case, there's\n", "no advantage to making the two coordinates into a single object.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(p[0], p[1], marker='o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we'll plot several points on the same diagram, using the\n", "parameters \"color\" and \"size.\" Here we just type the point locations\n", "directly into the plot functions rather than getting them from\n", "variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(2.6, 3.2, marker='o', color='blue', markersize=6);\n", "plt.plot(2.5, 3.1, marker='o', color='red', markersize=12);\n", "plt.plot(2.7, 3.15, marker='o', color='green', markersize=20);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Well, that didn't go very well. All our points ended up\n", "on the border of the diagram. We can fix this by specifying\n", "the \"limits\" of the diagram in both the x and y\n", "directions by using the plt object's \"xlim\" and \"ylim\" functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.xlim([2.2, 3.5])\n", "plt.ylim([3, 3.5])\n", "plt.plot(2.6, 3.2, marker='o', color='blue', markersize=6);\n", "plt.plot(2.5, 3.1, marker='o', color='red', markersize=12);\n", "plt.plot(2.7, 3.15, marker='o', color='green', markersize=20);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I can also put all three points in a single plot function, by making the first parameter a list of all the x coordinates and the\n", "second parameter a list of all the y coordinates. If I do\n", "this, then the plotter will draw a line between the points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.xlim([2.2, 3.5])\n", "plt.ylim([3, 3.5])\n", "plt.plot([2.6, 2.5, 2.7], [3.2, 3.1, 3.15], \n", " marker='o', color='black', markersize=6);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "

^ Lines

\n", "

\n", "So, if I want to draw a line, all I have to do is give the x and\n", "y coordinates for the points at each end of the line. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Draw a line between two points (1,1) and (2,3)\n", "# (Remember, the first two parameters are not the two points,\n", "# but rather the x coordinates and then the y coordinates.\n", "\n", "plt.plot([1, 2], [1, 3], marker='o', color='black')\n", "plt.xlim([0,4])\n", "plt.ylim([0,4]);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If I don't want to see the points, I just remove the \"marker\"\n", "# parameter.\n", "plt.plot([1, 2], [1, 3], color='black')\n", "plt.xlim([0,4])\n", "plt.ylim([0,4]);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "


\n", "\n", "

^ Curves

\n", "

\n", "Now we'll do what we really came here for which is to plot \"curves\"\n", "-- that is, functions!\n", "

\n", "The first function we'll plot will be the square root function. We\n", "will use the x-y coordinate system, and we'll say that:\n", "$$\n", "y = \\sqrt{x}\n", "$$\n", "I'm going to give a blow-by-blow commentary on almost every line of\n", "code here. Normally you would do all this in a single cell. So you need\n", "to make sure to run each of the following cells in order.\n", "

\n", "We're going to use two functions from the numpy library, so let's\n", "just make sure we've imported them:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy import linspace, sqrt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we use the \"linspace\" function. This basically creates a numeric\n", "\"x axis.\" The first parameter to linspace is the lowest value of x to\n", "use. The 2nd parameter is the highest x value, and the last parameter\n", "is the number of points. So, the variable \"x\" is not a single number,\n", "it is a list of five numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = linspace(0, 100, 5)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, in the cell below, it says: y = sqrt(x)\n", "But x is a list of numbers. This is where numpy comes in.\n", "We're using numpy's sqrt() function, and numpy functions operate\n", "on lists. So the variable y will be a list of numbers, each\n", "of which will be the square root of the corresponding x number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = sqrt(x)\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally, we can plot the numbers. The variable x now holds all the\n", "x coordinates and the variable y holds all the y coordinates. So we\n", "simply say:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We didn't give it a \"marker\" so it just drew the lines, and we didn't\n", "give it a color, so it just picked blue. If we want to see the individual\n", "values, we can specify a marker:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y, marker='o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, notice that the \"curve\" is not really a curve, but rather\n", "a set of straight lines between the individual points. What we are really\n", "doing is approximating a curve with a set of lines. In this case,\n", "it doesn't look very good (particularly the first couple of lines on\n", "the left hand side). The curve is not very \"smooth.\" We can fix that\n", "simply by using more points:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First a rather jagged curve with only 5 points in Blue\n", "x = linspace(0, 100, 5)\n", "y = sqrt(x)\n", "plt.plot(x, y, color='blue');\n", "# Then a much smoother curve with 100 points in green\n", "x = linspace(0, 100, 100)\n", "y = sqrt(x)\n", "plt.plot(x, y, color='green');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It turns out that 100 or so points is often a pretty good number\n", "for a typical mathematical function. But it does depend on the\n", "function in question. Of course, once you've got 100 points, you\n", "probably won't want to print out the lists, or plot the individual\n", "points on the graph. If you want to see some specific point, but\n", "still have a smooth curve, you can do the plot twice, with two\n", "different numbers of points:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This is to get some specific points\n", "x = linspace(0, 100, 11)\n", "y = sqrt(x)\n", "# linestyle=' ' (blank between the quotes) keeps the first plot\n", "# from drawing a line, which would be jagged.\n", "plt.plot(x, y, color='black', marker='o', linestyle=' ');\n", "# And this is to do the curve\n", "x = linspace(0, 100, 100)\n", "y = sqrt(x)\n", "plt.plot(x, y, color='black');\n", "# Add a title at the top of the picture\n", "plt.title('Square Root Function');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are some more functions plotted:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy import pi\n", "x = linspace(0, 2*pi, 100)\n", "y = sin(x)\n", "plt.plot(x, y, color='black');\n", "plt.xlim(0, 2*pi)\n", "plt.ylim(-1.2, 1.2)\n", "plt.title('Sine Wave');" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = linspace(-2, 2, 100)\n", "y = x**2\n", "plt.plot(x, y, color='black');\n", "plt.title('y = x Squared');" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = linspace(-1.5, 2.5, 100)\n", "y = x**3 - 2*x**2 -x + 2\n", "plt.plot(x, y, color='black');\n", "plt.xlim(-1.5, 2.5)\n", "plt.ylim(-3, 3)\n", "plt.title('$y = x^3-2x^2-x+2$', fontsize=16);\n", "# This last thing is just to draw a line along the x axis\n", "# so that it's easier to see where the polynomial crosses zero\n", "plt.plot([-1.5, 2.5], [0, 0], color='gray');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Below is \"y\" from problem 25 from 3.5 of Stewart

\n", "

\n", "$y = \\frac{r}{\\sqrt{r^2+1}}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy import sqrt\n", "r = linspace(-10, 10, 100)\n", "y = r/sqrt(r**2+1)\n", "plt.plot(r,y);" ] }, { "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.5" } }, "nbformat": 4, "nbformat_minor": 1 }