Demo1

AMath 585, Winter Quarter 2020 at the University of Washington. Developed by R.J. LeVeque and distributed under the BSD license. You are free to modify and use as you please, with attribution.

These notebooks are all available on Github.

This notebook just illustrates a few things to make simple plots.

Markdown

Note that some cells are executable code cells, others are Markdown cell, which display text and in which you can use latex for mathematics. The notebooks use Github flavored markdown. Double click on a markdown cell to edit it, then execute it to render.

Make plots appear inline:

The following notebook "magic" command %matplotlib inline makes plots show up in the notebook rather than opening a new window.

Instead you can specify %matplotlib notebook so they show up in a manner that you can interact with them, e.g. zooming in. If you do this then when you give plot commands in more than one cell you should close earlier figures before plotting new ones (or your plot will be sent to the previous figure), or start each such cell with a figure command. If you have too many figures open at once in this mode, you may get an error message telling you to close some. Closing a figure will leave the figure in the notebook, but it's no longer interactive.

In [1]:
%matplotlib inline

Import a bunch of useful functions

The next line imports many things from numpy and matplotlib, including things like pi, sin, linspace, and plot that are used below.

In [2]:
from pylab import *
In [3]:
x = linspace(0, 4*pi, 100)
In [4]:
y = x*x + x**3*sin(x)

Make a plot

Here is a plot of $f(x) = x^2 + x^3\sin(x)$.

In [5]:
figure(figsize=(6,4))
plot(x,y,'r')
Out[5]:
[<matplotlib.lines.Line2D at 0x11a830a20>]

Make another version that has different colored points and line.

Also save the result as a png file that can be downloaded.

In [6]:
x = linspace(0, 4*pi, 50)
y = x*x + x**3*sin(x)

figure(figsize=(6,4))
plot(x, y, 'r-')
plot(x, y, 'bo')

title('My plot')
grid(True)
savefig('myplot.png')

We can also display this png file (or any other image) inline:

In [7]:
from IPython.display import Image
Image('myplot.png')
Out[7]:

For help with plotting:

See the matplotlib gallery for many examples.