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.
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.
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.
%matplotlib inline
The next line imports many things from numpy and matplotlib, including things like pi
, sin
, linspace
, and plot
that are used below.
from pylab import *
x = linspace(0, 4*pi, 100)
y = x*x + x**3*sin(x)
Here is a plot of $f(x) = x^2 + x^3\sin(x)$.
figure(figsize=(6,4))
plot(x,y,'r')
Make another version that has different colored points and line.
Also save the result as a png
file that can be downloaded.
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:
from IPython.display import Image
Image('myplot.png')
See the matplotlib gallery for many examples.