CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
import numpy as np
2
import pylab as plt
3
from matplotlib.patches import Circle, Rectangle, Polygon, Arrow, FancyArrow
4
import book_plots
5
6
def create_predict_update_chart(box_bg = '#CCCCCC',
7
arrow1 = '#88CCFF',
8
arrow2 = '#88FF88'):
9
plt.figure(figsize=(4,4), facecolor='w')
10
ax = plt.axes((0, 0, 1, 1),
11
xticks=[], yticks=[], frameon=False)
12
#ax.set_xlim(0, 10)
13
#ax.set_ylim(0, 10)
14
15
16
pc = Circle((4,5), 0.5, fc=box_bg)
17
uc = Circle((6,5), 0.5, fc=box_bg)
18
ax.add_patch (pc)
19
ax.add_patch (uc)
20
21
22
plt.text(4,5, "Predict\nStep",ha='center', va='center', fontsize=14)
23
plt.text(6,5, "Update\nStep",ha='center', va='center', fontsize=14)
24
25
#btm
26
ax.annotate('',
27
xy=(4.1, 4.5), xycoords='data',
28
xytext=(6, 4.5), textcoords='data',
29
size=20,
30
arrowprops=dict(arrowstyle="simple",
31
fc="0.6", ec="none",
32
patchB=pc,
33
patchA=uc,
34
connectionstyle="arc3,rad=-0.5"))
35
#top
36
ax.annotate('',
37
xy=(6, 5.5), xycoords='data',
38
xytext=(4.1, 5.5), textcoords='data',
39
size=20,
40
arrowprops=dict(arrowstyle="simple",
41
fc="0.6", ec="none",
42
patchB=uc,
43
patchA=pc,
44
connectionstyle="arc3,rad=-0.5"))
45
46
47
ax.annotate('Measurement ($\mathbf{z_k}$)',
48
xy=(6.3, 5.4), xycoords='data',
49
xytext=(6,6), textcoords='data',
50
size=18,
51
arrowprops=dict(arrowstyle="simple",
52
fc="0.6", ec="none"))
53
54
ax.annotate('',
55
xy=(4.0, 3.5), xycoords='data',
56
xytext=(4.0,4.5), textcoords='data',
57
size=18,
58
arrowprops=dict(arrowstyle="simple",
59
fc="0.6", ec="none"))
60
61
ax.annotate('Initial\nConditions ($\mathbf{x_0}$)',
62
xy=(4.0, 5.5), xycoords='data',
63
xytext=(2.5,6.5), textcoords='data',
64
size=18,
65
arrowprops=dict(arrowstyle="simple",
66
fc="0.6", ec="none"))
67
68
plt.text (4,3.4,'State Estimate ($\mathbf{\hat{x}_k}$)',
69
ha='center', va='center', fontsize=18)
70
plt.axis('equal')
71
#plt.axis([0,8,0,8])
72
plt.show()
73
74
75
def plot_estimate_chart_1():
76
ax = plt.axes()
77
ax.annotate('', xy=[1,159], xytext=[0,158],
78
arrowprops=dict(arrowstyle='->', ec='r',shrinkA=6, lw=3,shrinkB=5))
79
plt.scatter ([0], [158], c='b')
80
plt.scatter ([1], [159], c='r')
81
plt.xlabel('day')
82
plt.ylabel('weight (lbs)')
83
plt.show()
84
85
86
def plot_estimate_chart_2():
87
ax = plt.axes()
88
ax.annotate('', xy=[1,159], xytext=[0,158],
89
arrowprops=dict(arrowstyle='->',
90
ec='r', lw=3, shrinkA=6, shrinkB=5))
91
plt.scatter ([0], [158.0], c='k',s=128)
92
plt.scatter ([1], [164.2], c='b',s=128)
93
plt.scatter ([1], [159], c='r', s=128)
94
plt.text (1.0, 158.8, "prediction ($x_t)$", ha='center',va='top',fontsize=18,color='red')
95
plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
96
plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)
97
plt.xlabel('day')
98
plt.ylabel('weight (lbs)')
99
plt.show()
100
101
def plot_estimate_chart_3():
102
ax = plt.axes()
103
ax.annotate('', xy=[1,159], xytext=[0,158],
104
arrowprops=dict(arrowstyle='->',
105
ec='r', lw=3, shrinkA=6, shrinkB=5))
106
107
ax.annotate('', xy=[1,159], xytext=[1,164.2],
108
arrowprops=dict(arrowstyle='-',
109
ec='k', lw=1, shrinkA=8, shrinkB=8))
110
111
est_y = ((164.2-158)*.8 + 158)
112
plt.scatter ([0,1], [158.0,est_y], c='k',s=128)
113
plt.scatter ([1], [164.2], c='b',s=128)
114
plt.scatter ([1], [159], c='r', s=128)
115
plt.text (1.0, 158.8, "prediction ($x_t)$", ha='center',va='top',fontsize=18,color='red')
116
plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
117
plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)
118
plt.text (0.95, est_y, "new estimate ($\hat{x}_{t}$)", ha='right', va='center',fontsize=18)
119
plt.xlabel('day')
120
plt.ylabel('weight (lbs)')
121
plt.show()
122
123
def plot_hypothesis():
124
plt.errorbar([1, 2, 3], [170, 161, 169],
125
xerr=0, yerr=10, fmt='bo', capthick=2, capsize=10)
126
127
plt.plot([1, 3], [180, 160], color='g', ls='--')
128
plt.plot([1, 3], [170, 170], color='g', ls='--')
129
plt.plot([1, 3], [160, 175], color='g', ls='--')
130
plt.plot([1, 2, 3], [180, 152, 179], color='g', ls='--')
131
plt.xlim(0,4); plt.ylim(150, 185)
132
plt.xlabel('day')
133
plt.ylabel('lbs')
134
plt.tight_layout()
135
plt.show()
136
137
def plot_hypothesis2():
138
plt.errorbar(range(1, 11), [169, 170, 169,171, 170, 171, 169, 170, 169, 170],
139
xerr=0, yerr=6, fmt='bo', capthick=2, capsize=10)
140
plt.plot([1, 10], [169, 170.5], color='g', ls='--')
141
plt.xlim(0, 11); plt.ylim(150, 185)
142
plt.xlabel('day')
143
plt.ylabel('lbs')
144
plt.show()
145
146
147
def plot_hypothesis3():
148
weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,
149
169.6, 167.4, 166.4, 171.0, 171.2, 172.6]
150
plt.errorbar(range(1, 13), weights,
151
xerr=0, yerr=6, fmt='o', capthick=2, capsize=10)
152
153
plt.xlim(0, 13); plt.ylim(145, 185)
154
plt.xlabel('day')
155
plt.ylabel('weight (lbs)')
156
plt.show()
157
158
159
def plot_hypothesis4():
160
weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,
161
169.6, 167.4, 166.4, 171.0, 171.2, 172.6]
162
163
ave = np.sum(weights) / len(weights)
164
plt.errorbar(range(1,13), weights, label='weights',
165
yerr=6, fmt='o', capthick=2, capsize=10)
166
plt.plot([1, 12], [ave,ave], c='r', label='hypothesis')
167
plt.xlim(0, 13); plt.ylim(145, 185)
168
plt.xlabel('day')
169
plt.ylabel('weight (lbs)')
170
book_plots.show_legend()
171
plt.show()
172
173
174
def plot_hypothesis5():
175
weights = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6,
176
169.6, 167.4, 166.4, 171.0, 171.2, 172.6]
177
178
xs = range(1, len(weights)+1)
179
line = np.poly1d(np.polyfit(xs, weights, 1))
180
plt.errorbar(range(1, 13), weights, label='weights',
181
yerr=5, fmt='o', capthick=2, capsize=10)
182
plt.plot (xs, line(xs), c='r', label='hypothesis')
183
plt.xlim(0, 13); plt.ylim(145, 185)
184
plt.xlabel('day')
185
plt.ylabel('weight (lbs)')
186
book_plots.show_legend()
187
plt.show()
188
189
def plot_g_h_results(measurements, filtered_data,
190
title='', z_label='Measurements', **kwargs):
191
book_plots.plot_filter(filtered_data, **kwargs)
192
book_plots.plot_measurements(measurements, label=z_label)
193
book_plots.show_legend()
194
plt.title(title)
195
plt.gca().set_xlim(left=0,right=len(measurements))
196
197
if __name__ == '__main__':
198
create_predict_update_chart()
199