Demo1

AMath 586, Spring Quarter 2019 at the University of Washington. For other notebooks, see Index.html or the Index of all notebooks on Github.

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

Import a bunch of stuff and make plots appear inline:

The following notebook "magic" command makes plots show up in the notebook in a manner that you can interact with them, e.g. zooming in. If you give plot commands in more than one cell you should close earlier figures before plotting new ones, or start each such cell with a figure command.

(Instead use %matplotlib inline to get them to show up in notebook but not be interactive.)

In [1]:
%matplotlib notebook

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 0x11f1c4070>]

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.