Python III: Plotting and visual presentation


References


Plotting

Basics

Python also has a useful set of plotting tools. The most commonly used and basic tools for plotting are from the matplotlib.pyplot package. There is an excellent gallery of examples. For a simple place to start, let us plot your solution to the least-squares problem, allong with the data, using Canopy's editor window. (use the m and b you calculated from the least-squares problem in lab II)

from matplotlib.pyplot import *

xdata = [0, 9, 5, 6]
ydata = [0, 9, 7, 5]
plot(xdata, ydata)
show()

This doesn't really give us what we want -- we'd like points rather than a line. The plot function uses the same stylies for drawing lines that Octave and Matlab do.

So, to draw points as magenta circles with no connecting line, first we clear the figure using the clf() command. Then we redraw it.

clf()
plot(xdata, ydata, 'mo')
show()

Still, all four points are not clearly visible -- they are small and stuck in the corners. We can fix this a little by manually elarging the limits on the domain and range of the plot, and by increasing the size of the points.

clf()
plot(xdata, ydata, 'mo', markersize=10)
xlim(-1,10)
ylim(-1,10)
show()

Now, that looks good. Let's draw in your regression line now to see how it fits the data. (remember to fill in your values for m and b.

m = ?
b = ?
from numpy import linspace
x = linspace(0,10,3)
y = m * x + b
clf()
plot(xdata, ydata, 'mo', x, y, 'k--', markersize=10, linewidth=2)
show()

Now, add title, an x-label, and a y-label to your plot. Make sure the font sizes are large enough to be easily read, even if the figure is shrunk. Save your figure to a pdf image file with the savefig command.

One extra useful hint. Often the data you want to plot are in point-form like pts = [ (0,0), (9,9), (5,7), (6,5) ] rather than the x-vector/y-vector form used by plot. The fastest way to convert from point-form to vector-form is with the zip command.

pts = [ (0,0), (9,9), (5,7), (6,5) ]
xdata, ydata = zip( * pts)

We can also plot data we've stored in a file. For example, this early data collected by George Cayley on lift in relation to the angle of attach of a wing.

figure() # to create a NEW figure
data = loadtxt('CayleyLiftData.csv', delimiter=',')
plot(data[:6,1],data[:6,2],'o-')
plot(data[6:,1],data[6:,2],'x-')
legend(['15 feet per sec', '21.8 feet per sec'])
ylim(0,0.8)
xlim(0,25)

There are other styles of plots as well. For example, you can plot images with imshow.

figure(2)
imshow(randn(20,30), interpolation='none')
colorbar()

Types of images

There are allot of different image formats you can choose from for saving your plots. Each has it's own good and bad sides.

Python Notebooks

To include plots in one of Jupyter's python notebooks, use the macro %matpotlib inline at the top of the notebook. This tells Jupyter to include the plots in the notebook as they are drawn, rather than as seperate windows (which many browsers don't allow). Try the following in a notebook.

%matplotlib inline

r = 3.8
f = lambda x, n : ([x] if n == 0 else [x]+f(r*x*(1-x), n - 1))

n = 50
y = f(0.5,n)
plot(range(len(y)), y, 'bo-')
ylim(0,1)
xlabel('Time')
ylabel('Fishery Stock Size');

Ordinary differential equation methods

Differential equations are some of the most useful ways of scribing natural systems and lead to some very interesting insights. The next examples show how to solve and analyze Ordinary differential equation systems with python.

Example 1: Exponential decay

The simplest autonomous ordinary differential equation of interest in science is the exponential decay equation

dn/dt=rnwhere r<0.

Implement the following script

# Numerical integration of the exponential decay model
#
#       dn/dt = r n, where r < 0
#

from numpy import linspace, array
import scipy.integrate
from scipy import exp
from matplotlib.pyplot import \
        plot, figure, text, show, savefig, xlabel, ylabel

r = -0.05 # decay rate per day

def vectorField(X, t):
    n = X[0]
    return array([ r * n ])


timeStart = 0.
timeEnd = 10.
numsteps = 20

observationTimes = linspace(timeStart, timeEnd, numsteps)

Xinitial = array([32.])

X = scipy.integrate.odeint(vectorField, Xinitial, observationTimes)

XFinalCalc = X[-1, 0]
XFinalExct = Xinitial[0]*exp(r*timeEnd)
XFinalError = abs( XFinalCalc - XFinalExct )
errorStr = "Final Error = %1.5g"%(XFinalError)

figure(1)
options = {'fontsize': 18}

plot(observationTimes, X[:, 0], 'x-')
xlabel('Time (days)', options)
ylabel('Mass (micrograms)', options)
text(2, 20, errorStr, options)
savefig('exponential.pdf')
show()

Example 2: The dampled pendulum

A pendulum on a rod, hanging from a pivot with weak friction should move according to the following system of equations, according to Newton's laws.

dθdt=ω,dωdt=sin(θ)aω.

The angle is measured as different from the rest position. In our case, let's assume the friction coefficient is a=0.05.

Example 3: Watt's governor

Diagram of Watt's centrifugal flying-ball governor

Diagram of Watt's centrifugal flying-ball governor

References

Watt's governor was the first automatic control system used at the beginning of the industrial revolution for steam engines. It opened up whole new fields of both engineering and dynamical system analysis. In particular, James Maxwell showed that these governors could become unstable. Here are there equations, which we'll study a little.

dxdt=ydydt=z2sin(x)cos(x)sin(x)fydzdt=a(cos(x)b)parameter valuesa=1,b=1/2initial conditionx(0)=1.0472,y(0)=0.,z(0)=1.386 Here, the state (x,y,z)[0,π]×(,)×[0,). where

The parameters (a,b,f)[0,)×[0,a]×[0,). where

Analysis

  1. I. A. Vyshnegradskii and J. C. Maxwell independently showed that if the friction f is large enough, then the governor settles on a stable control. Plot timeseries for x,y,z when f=4/5 over 100 seconds.

  2. But if f is small enough, then the governor "hunts" for the best level without ever settling! Plot timeseries for x,y,z when f=1/2, and describe how the behavior differs from the previous case.

  3. Enter the vector field system into a column vector in sympy. Use sympy to solve for the steady-states of the system. Then, numerically estimate the steady-states for (a,b,f)=(1,1/2,4/5) and (a,b,f)=(1,1/2,1/2).

  4. Create a new function called "jacobian" to calculate the Jacob's derivative matrix of a vector field (given by a sequence of equations es) in terms of some variables (give by a sequence xs)

    >>> jacobian = lambda es, xs : Matrix([[ e.diff(x) for x in xs] for e in es])

    Use your new function to calculate the Jacobian of vector field for Watt's governor.

  5. Using the results of your calculates so far, create a new python the determines the sign of the largest eigenvalue of the Jacobian when evaluated at the steady-state solution. Numerically estimate the steady-states eigenvalues at steady-state for parameter sets (a,b,f)=(1,1/2,4/5) and (a,b,f)=(1,1/2,1/2). In which case is there an exponentially-growing part to solutions near the steady-state? At what value of f does this eigenvalue switch from having positive real part instead of a negative real part?

Example 4: Behavior Change in an epidemic

A simple extension of classic epidemic theory is a case where resistance to disease is behavioral, and individuals who become sick tend to abandon their protective behaviors. This kind of phenomena can be described with the system dSdt=μβSI+fγI+aRvSμS,dIdt=β(S+σR)IγIμI,dRdt=σβRI+(1f)γIaR+vSμR. where S is the fraction of susceptibles, I is the fraction of infecteds, and R is the fraction of recovered people (1=S+I+R). Calculate all steady-states when f=9/10, γ=61, a=1/6, μ=1/78, β=570, ν=1/3, and determine their local stability.

Example 5: Autocatalytic reactions

Once upon a time, it was believed that chemical reactions would always got to equilibrium straight-away, but some visionary chemists eventually showed that, in fact, chemical reactions can oscillate. The simpliest mathematical model of this phenomena is given by the bi-molecular mass-action reaction equations dpdt=ap+p2qdqdt=bp2q where a and b are positive constants.

  1. Determine the steady-states of this system

  2. Find values of a and b for which the system is unstable and oscillates. Show with a time-series plot.

  3. Find different values of a and b for which the system is stable and converges to equilibrium. Show with a time-series plot.