Week Seven: Plotting
Plotting
-
Visualization of data
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # first trial plt.plot(mySamples, myLinear) plt.plot(mySamples, myQuadratic) plt.plot(mySamples, myCubic) plt.plot(mySamples, myExponential)
-
Different Plots
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # second trial plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential)
-
label & title & clf
-
label:横纵轴标签
-
title:表格标题
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # second trial plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential) plt.figure('lin') plt.xlabel('Sample Points') plt.ylabel('myLinear') plt.title('Linear') plt.figure('quad') plt.xlabel('Sample Points') plt.ylabel('myQuad') plt.title('Quad')
-
clf:清除表格信息 ```python import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = []
for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i2) myCubic.append(i3) myExponential.append(1.5**i)
plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential) plt.figure('lin') plt.xlabel('Sample Points') plt.ylabel('myLinear') plt.title('Linear') plt.figure('quad') plt.xlabel('Sample Points') plt.ylabel('myQuad') plt.title('Quad') plt.figure('quad') plt.clf() #清除表格信息
```
-
-
lim & Overlaying & Legend
-
lim:限制横纵轴参数
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # second trial plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential) plt.figure('lin') plt.xlabel('Sample Points') plt.ylabel('myLinear') plt.title('Linear') plt.figure('quad') plt.xlabel('Sample Points') plt.ylabel('myQuad') plt.title('Quad') plt.figure('quad') plt.clf() plt.ylim(0,1000) plt.plot(mySamples, myQuadratic)
-
overlaying:比较两个图表
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # second trial plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential) plt.figure('lin') plt.xlabel('Sample Points') plt.ylabel('myLinear') plt.title('Linear') plt.figure('quad') plt.xlabel('Sample Points') plt.ylabel('myQuad') plt.title('Quad') plt.figure('quad') plt.clf() plt.ylim(0,1000) plt.plot(mySamples, myQuadratic) plt.figure('lin quad') plt.clf() plt.plot(mySamples, myLinear) plt.plot(mySamples, myQuadratic) plt.title('lin vs. quad')
-
legend:图例
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) # second trial plt.figure('lin') plt.plot(mySamples, myLinear) plt.figure('quad') plt.plot(mySamples, myQuadratic) plt.figure('cube') plt.plot(mySamples, myCubic) plt.figure('expo') plt.plot(mySamples, myExponential) plt.figure('lin') plt.xlabel('Sample Points') plt.ylabel('myLinear') plt.title('Linear') plt.figure('quad') plt.xlabel('Sample Points') plt.ylabel('myQuad') plt.title('Quad') plt.figure('quad') plt.clf() plt.ylim(0,1000) plt.plot(mySamples, myQuadratic) plt.figure('lin quad') plt.clf() plt.plot(mySamples, myLinear) plt.plot(mySamples, myQuadratic) plt.title('lin vs. quad') plt.figure('lin quad') plt.clf() plt.plot(mySamples, myLinear, label = 'linear') plt.plot(mySamples, myQuadratic, label = 'Quadratic') plt.legend(loc = 'upper left')
-
-
Changing Data Display
-
color & style:
-
:lineo
:circle^
:triange--
:dash
-
linewidth:线宽
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) plt.figure('lin quad') plt.clf() plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth = 4.0) plt.plot(mySamples, myQuadratic, 'r--', label = 'quadratic', linewidth = 1.0)
-
subplot
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) plt.figure('lin quad') plt.clf() plt.subplot(211) plt.ylim(0,900) plt.plot(mySamples, myLinear, 'g-', label = 'linear', linewidth = 2.0) plt.subplot(212) plt.ylim(0,1000) plt.plot(mySamples, myQuadratic, 'r--', label = 'quadratic', linewidth = 1.0)
-
scale
import pylab as plt mySamples = [] myLinear = [] myQuadratic = [] myCubic = [] myExponential = [] for i in range(0,30): mySamples.append(i) myLinear.append(i) myQuadratic.append(i**2) myCubic.append(i**3) myExponential.append(1.5**i) plt.figure('cube exp log') plt.clf() plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth = 2.0) plt.plot(mySamples, myExponential, 'r', label = 'exponential', linewidth = 4.0) plt.yscale('log') plt.legend() plt.title('Cubic vs. Exponential') plt.figure('cube exp linear') plt.clf() plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth = 2.0) plt.plot(mySamples, myExponential, 'r', label = 'exponential', linewidth = 4.0) plt.legend() plt.title('Cubic vs. Exponential')
-
-
An Example—Retirement Saving
-
compound interest
import pylab as plt def retirement(monthly, rate, terms): base = [0] value = [0] mrate = rate/12 for i in range(terms): base += [i] value += [value[-1] * (1 + mrate) + monthly] return base, value
-
display retirement
def displayretirement(monthlies, rate, terms): plt.figure('retirement') plt.clf for i in monthlies: xvalue,yvalue = retirement(i, rate, terms) plt.plot(xvalue,yvalue, label = "retire:" + str(i)) plt.legend(loc='upper left') displayretirement([400, 600, 800], 0.03, 30*12)
-
display retirement rates
def displayretirementrate(monthly, rates, terms): plt.figure('retirement rate') plt.clf for i in rates: xvalue,yvalue = retirement(monthly, i, terms) plt.plot(xvalue,yvalue, label = "retirerate:" + str(i)) plt.legend(loc = 'upper left') displayretirementrate(600, [0.03, 0.04, 0.05], 30*12)
-
display both retirement
def displaybothretirement(monthlies, rates, terms): plt.figure('retirement monthlies rate') plt.clf plt.xlim(25*12, 30*12) monthlabels = ['r', 'b', 'g','k'] ratelabels = ['-', 'o', '--'] for i in range(len(monthlies)): month = monthlies[i] monthlabel = monthlabels[i % len(monthlies)] #循环赋值 for j in range(len(rates)): rate = rates[j] ratelabel = ratelabels[j % len(ratelabels)] #循环赋值 xvalue,yvalue = retirement(month, rate, terms) plt.plot(xvalue,yvalue, monthlabel+ratelabel, label = "retire:" + str(month) + ':' +str(rate) ) #赋值线段属性 plt.legend(loc = 'upper left') displaybothretirement([400, 600, 800, 1000], [0.03, 0.04, 0.05], 30*12)
-
-
Course Summary
-
Topics covered
-
Computational thinking
-
The process
-
3 As
-
Think recursively!!!
-
-
One more thing
-