Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: math480-2016
Views: 2158

Math 480: Open Source Mathematical Software

2016-05-20

William Stein

Lectures 24: Matplotlib Pyplot vs Sage vs MATLAB

Notes:

  1. Homework and peer grading due at 6pm tonight.

  2. I wrote a lot more about numpy on Wednesday; read it yourself.

  3. (Start screencast!) Today -- matplotlib for a while

  4. Then work on homework

We follow http://matplotlib.org/users/pyplot_tutorial.html, and shows how to use the MATLAB-stype interface to matplotlib. We will cover topics:

  • plot: plotting a list of values

  • array of plots: axes and figures

%auto import numpy as np import matplotlib.pyplot as plt

1. Plot

plt.plot([1,2,3,10]) plt.show()
[<matplotlib.lines.Line2D object at 0x7f1713324fd0>]

MPL's plt.plot is VERY different than Sage's plot!

plt.plot(x values, y yalues, options, [repeat])

plt.plot([1,2,3,10], [0,2,4,6], 'ro-') plt.show()
[<matplotlib.lines.Line2D object at 0x7f17133627d0>]

In Sage, you use the line command instead, and have to put the points together (using zip).

Also, in Sage, use frame=True to draw a frame instead of x,yx,y-axes.

show( line(zip([1,2,3,10], [0,2,4,6]), color='red', marker='o'), frame=True)
# Another example with two curves: x = np.linspace(-2*np.pi, 2*np.pi) # remember from wednesday y1 = np.sin(x) # apply function to array is entry wise. y2 = np.cos(x) plt.plot(x,y1, x,y2) # give x and corresponding y's plt.show()
[<matplotlib.lines.Line2D object at 0x7f17131778d0>, <matplotlib.lines.Line2D object at 0x7f1713122d90>]

This is similar to MATLAB! -- see http://www.mathworks.com/help/matlab/ref/plot.html

Guess what this will do?

t = np.arange(0, 5, 0.2) plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()

What will this do?

plt.plot(t, t**3 + t, 'g', linewidth=3) plt.show()

Exercise: Plot the polynomial x4+2x1x^4 + 2x - 1 using both matplotlib and Sage's plot commands, for 3x3-3\leq x \leq 3.

︠cbdc1d00-2840-4627-84dc-f2fc7d5d70d9︠ ︠d3bcd36b-97a2-4ef9-b92e-6865325f82c2i︠ %md ### 2. Figures and Axes

2. Figures and Axes

def f(t): return np.exp(-t) * np.cos(2*np.pi*t) def makefigure(): t1 = np.arange(0, 5, 0.1) t2 = np.arange(0, 5, 0.02) plt.subplot(221) # 2=numrows, 1=numcols, 1=fignum (which is from 1 to numrows*numcols) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(222) # 2=numrows, 1=numcols, 2=fignum... (yes, MATLAB is horrible.) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.subplot(223) # 2=numrows, 1=numcols, 2=fignum... (yes, MATLAB is horrible.) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.subplot(224) # 2=numrows, 1=numcols, 2=fignum... (yes, MATLAB is horrible.) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show() makefigure()

Exercise: Copy the above code and instead make a 2x4 grid of plots (instead of a 2x1 grid). Just put whatever plots you want in.

def f(t): return np.exp(-t) * np.cos(2*np.pi*t) def makefigure(): t1 = np.arange(0, 5, 0.1) t2 = np.arange(0, 5, 0.02) # MODIFY THIS CODE: plt.subplot(211) # 2=numrows, 1=numcols, 1=fignum (which is from 1 to numrows*numcols) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) # 2=numrows, 1=numcols, 2=fignum... (yes, MATLAB is horrible.) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show() makefigure()

In Sage, there is graphics_array (inspired by Mathematica...) to do the above grid layout.

%var t g1 = plot(exp(-t)*cos(2*pi*t), 0, 5) g2 = plot(cos(2*pi*t), 0, 5) show(graphics_array([[g1,g2,g1], [g2,g1,g1]]), frame=True)

Exercise: modify the above code to make a 2x2 array of plots using Sage plotting.

# EDIT ME! %var t g1 = plot(exp(-t)*cos(2*pi*t), 0, 5) g2 = plot(cos(2*pi*t), 0, 5) show(graphics_array([[g1], [g2]]), frame=True) ︠1e49deff-fbab-4328-bbe8-26b592adc28e︠