Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872import numpy as np1import pylab as plt2from matplotlib.patches import Circle, Rectangle, Polygon, Arrow, FancyArrow3import book_plots45def create_predict_update_chart(box_bg = '#CCCCCC',6arrow1 = '#88CCFF',7arrow2 = '#88FF88'):8plt.figure(figsize=(4,4), facecolor='w')9ax = plt.axes((0, 0, 1, 1),10xticks=[], yticks=[], frameon=False)11#ax.set_xlim(0, 10)12#ax.set_ylim(0, 10)131415pc = Circle((4,5), 0.5, fc=box_bg)16uc = Circle((6,5), 0.5, fc=box_bg)17ax.add_patch (pc)18ax.add_patch (uc)192021plt.text(4,5, "Predict\nStep",ha='center', va='center', fontsize=14)22plt.text(6,5, "Update\nStep",ha='center', va='center', fontsize=14)2324#btm25ax.annotate('',26xy=(4.1, 4.5), xycoords='data',27xytext=(6, 4.5), textcoords='data',28size=20,29arrowprops=dict(arrowstyle="simple",30fc="0.6", ec="none",31patchB=pc,32patchA=uc,33connectionstyle="arc3,rad=-0.5"))34#top35ax.annotate('',36xy=(6, 5.5), xycoords='data',37xytext=(4.1, 5.5), textcoords='data',38size=20,39arrowprops=dict(arrowstyle="simple",40fc="0.6", ec="none",41patchB=uc,42patchA=pc,43connectionstyle="arc3,rad=-0.5"))444546ax.annotate('Measurement ($\mathbf{z_k}$)',47xy=(6.3, 5.4), xycoords='data',48xytext=(6,6), textcoords='data',49size=18,50arrowprops=dict(arrowstyle="simple",51fc="0.6", ec="none"))5253ax.annotate('',54xy=(4.0, 3.5), xycoords='data',55xytext=(4.0,4.5), textcoords='data',56size=18,57arrowprops=dict(arrowstyle="simple",58fc="0.6", ec="none"))5960ax.annotate('Initial\nConditions ($\mathbf{x_0}$)',61xy=(4.0, 5.5), xycoords='data',62xytext=(2.5,6.5), textcoords='data',63size=18,64arrowprops=dict(arrowstyle="simple",65fc="0.6", ec="none"))6667plt.text (4,3.4,'State Estimate ($\mathbf{\hat{x}_k}$)',68ha='center', va='center', fontsize=18)69plt.axis('equal')70#plt.axis([0,8,0,8])71plt.show()727374def plot_estimate_chart_1():75ax = plt.axes()76ax.annotate('', xy=[1,159], xytext=[0,158],77arrowprops=dict(arrowstyle='->', ec='r',shrinkA=6, lw=3,shrinkB=5))78plt.scatter ([0], [158], c='b')79plt.scatter ([1], [159], c='r')80plt.xlabel('day')81plt.ylabel('weight (lbs)')82plt.show()838485def plot_estimate_chart_2():86ax = plt.axes()87ax.annotate('', xy=[1,159], xytext=[0,158],88arrowprops=dict(arrowstyle='->',89ec='r', lw=3, shrinkA=6, shrinkB=5))90plt.scatter ([0], [158.0], c='k',s=128)91plt.scatter ([1], [164.2], c='b',s=128)92plt.scatter ([1], [159], c='r', s=128)93plt.text (1.0, 158.8, "prediction ($x_t)$", ha='center',va='top',fontsize=18,color='red')94plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')95plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)96plt.xlabel('day')97plt.ylabel('weight (lbs)')98plt.show()99100def plot_estimate_chart_3():101ax = plt.axes()102ax.annotate('', xy=[1,159], xytext=[0,158],103arrowprops=dict(arrowstyle='->',104ec='r', lw=3, shrinkA=6, shrinkB=5))105106ax.annotate('', xy=[1,159], xytext=[1,164.2],107arrowprops=dict(arrowstyle='-',108ec='k', lw=1, shrinkA=8, shrinkB=8))109110est_y = ((164.2-158)*.8 + 158)111plt.scatter ([0,1], [158.0,est_y], c='k',s=128)112plt.scatter ([1], [164.2], c='b',s=128)113plt.scatter ([1], [159], c='r', s=128)114plt.text (1.0, 158.8, "prediction ($x_t)$", ha='center',va='top',fontsize=18,color='red')115plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')116plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)117plt.text (0.95, est_y, "new estimate ($\hat{x}_{t}$)", ha='right', va='center',fontsize=18)118plt.xlabel('day')119plt.ylabel('weight (lbs)')120plt.show()121122def plot_hypothesis():123plt.errorbar([1, 2, 3], [170, 161, 169],124xerr=0, yerr=10, fmt='bo', capthick=2, capsize=10)125126plt.plot([1, 3], [180, 160], color='g', ls='--')127plt.plot([1, 3], [170, 170], color='g', ls='--')128plt.plot([1, 3], [160, 175], color='g', ls='--')129plt.plot([1, 2, 3], [180, 152, 179], color='g', ls='--')130plt.xlim(0,4); plt.ylim(150, 185)131plt.xlabel('day')132plt.ylabel('lbs')133plt.tight_layout()134plt.show()135136def plot_hypothesis2():137plt.errorbar(range(1, 11), [169, 170, 169,171, 170, 171, 169, 170, 169, 170],138xerr=0, yerr=6, fmt='bo', capthick=2, capsize=10)139plt.plot([1, 10], [169, 170.5], color='g', ls='--')140plt.xlim(0, 11); plt.ylim(150, 185)141plt.xlabel('day')142plt.ylabel('lbs')143plt.show()144145146def plot_hypothesis3():147weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,148169.6, 167.4, 166.4, 171.0, 171.2, 172.6]149plt.errorbar(range(1, 13), weights,150xerr=0, yerr=6, fmt='o', capthick=2, capsize=10)151152plt.xlim(0, 13); plt.ylim(145, 185)153plt.xlabel('day')154plt.ylabel('weight (lbs)')155plt.show()156157158def plot_hypothesis4():159weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,160169.6, 167.4, 166.4, 171.0, 171.2, 172.6]161162ave = np.sum(weights) / len(weights)163plt.errorbar(range(1,13), weights, label='weights',164yerr=6, fmt='o', capthick=2, capsize=10)165plt.plot([1, 12], [ave,ave], c='r', label='hypothesis')166plt.xlim(0, 13); plt.ylim(145, 185)167plt.xlabel('day')168plt.ylabel('weight (lbs)')169book_plots.show_legend()170plt.show()171172173def plot_hypothesis5():174weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,175169.6, 167.4, 166.4, 171.0, 171.2, 172.6]176177xs = range(1, len(weights)+1)178line = np.poly1d(np.polyfit(xs, weights, 1))179plt.errorbar(range(1, 13), weights, label='weights',180yerr=5, fmt='o', capthick=2, capsize=10)181plt.plot (xs, line(xs), c='r', label='hypothesis')182plt.xlim(0, 13); plt.ylim(145, 185)183plt.xlabel('day')184plt.ylabel('weight (lbs)')185book_plots.show_legend()186plt.show()187188def plot_g_h_results(measurements, filtered_data,189title='', z_label='Measurements', **kwargs):190book_plots.plot_filter(filtered_data, **kwargs)191book_plots.plot_measurements(measurements, label=z_label)192book_plots.show_legend()193plt.title(title)194plt.gca().set_xlim(left=0,right=len(measurements))195196if __name__ == '__main__':197create_predict_update_chart()198199