Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 16658

Math 152: Intro to Mathematical Software

2017-03-03

Kiran Kedlaya; University of California, San Diego

adapted from lectures by William Stein, University of Washington

Lecture 22: Plotting: Matplotlib Pyplot vs Sage vs MATLAB

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

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],[0,1,2,3],[2,5,5,7], 'ro-') plt.show()
[<matplotlib.lines.Line2D object at 0x7f991e1704d0>, <matplotlib.lines.Line2D object at 0x7f991e1706d0>]
zip([2,3,4],[1,2,3],[0,0,1])
[(2, 1, 0), (3, 2, 0), (4, 3, 1)]

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],[0,1,2,3]), color='red', marker='o'), frame=True)
3D rendering not yet implemented
# Another example with two curves: x = np.linspace(-2*np.pi, 2*np.pi, 200) # 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 0x7f991e625a10>, <matplotlib.lines.Line2D object at 0x7f991e625ad0>]

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()
[<matplotlib.lines.Line2D object at 0x7f991ea14fd0>, <matplotlib.lines.Line2D object at 0x7f991ea14990>, <matplotlib.lines.Line2D object at 0x7f991ea39890>]

What will this do?

plt.plot(t, t**3 + t, 'g', linewidth=5) plt.show()
[<matplotlib.lines.Line2D object at 0x7f991e10d250>]

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

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(231) # 2=numrows, 2=numcols, 1=fignum (which is from 1 to numrows*numcols) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(232) # 2=numrows, 2=numcols, 2=fignum... (yes, MATLAB is horrible.) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.subplot(234) # 2=numrows, 2=numcols, 3=fignum plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.subplot(236) # 2=numrows, 2=numcols, 4=fignum 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 2x2 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 plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) # 2=numrows, 1=numcols, 2=fignum 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)
%md Adding text...
t = np.arange(0, 5, 0.2) plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.text(2, 40, 'this is some text') plt.xlabel("This is the x axis", fontsize=10, color="blue") plt.ylabel("And the y axis") plt.title("This is an example: $x^3+e^x$") plt.show()
[<matplotlib.lines.Line2D object at 0x7f991e6a5fd0>, <matplotlib.lines.Line2D object at 0x7f991e69f210>, <matplotlib.lines.Line2D object at 0x7f991e69f850>] <matplotlib.text.Text object at 0x7f991e4c1290> <matplotlib.text.Text object at 0x7f991e9e1350> <matplotlib.text.Text object at 0x7f991e3ea510> <matplotlib.text.Text object at 0x7f991eab7fd0>
t = range(1,500,20) u = [random() for i in t] plt.plot(t, u) plt.xscale('log') plt.show()
[<matplotlib.lines.Line2D object at 0x7f991e616d10>]