Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Fri May 2 12:21:40 2014
4
5
@author: rlabbe
6
"""
7
import matplotlib.pyplot as plt
8
import numpy as np
9
10
def plot_errorbars(bars, xlims):
11
12
i = 1.0
13
for bar in bars:
14
plt.errorbar([bar[0]], [i], xerr=[bar[1]], fmt='o', label=bar[2] , capthick=2, capsize=10)
15
i += 0.2
16
17
plt.ylim(0, 2)
18
plt.xlim(xlims[0], xlims[1])
19
show_legend()
20
plt.gca().axes.yaxis.set_ticks([])
21
plt.show()
22
23
24
25
26
def show_legend():
27
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
28
29
30
def bar_plot(pos, ylim=(0,1), title=None):
31
plt.cla()
32
ax = plt.gca()
33
x = np.arange(len(pos))
34
ax.bar(x, pos, color='#30a2da')
35
if ylim:
36
plt.ylim(ylim)
37
plt.xticks(x+0.4, x)
38
if title is not None:
39
plt.title(title)
40
41
42
def set_labels(title=None, x=None, y=None):
43
""" helps make code in book shorter. Optional set title, xlabel and ylabel
44
"""
45
if x is not None:
46
plt.xlabel(x)
47
if y is not None:
48
plt.ylabel(y)
49
if title is not None:
50
plt.title(title)
51
52
53
def set_limits(x, y):
54
""" helper function to make code in book shorter. Set the limits for the x
55
and y axis.
56
"""
57
58
plt.gca().set_xlim(x)
59
plt.gca().set_ylim(y)
60
61
62
def plot_measurements(xs, ys=None, color='r', lw=2, label='Measurements', **kwargs):
63
""" Helper function to give a consistant way to display
64
measurements in the book.
65
"""
66
67
plt.autoscale(tight=True)
68
'''if ys is not None:
69
plt.scatter(xs, ys, marker=marker, c=c, s=s,
70
label=label, alpha=alpha)
71
if connect:
72
plt.plot(xs, ys, c=c, lw=1, alpha=alpha)
73
else:
74
plt.scatter(range(len(xs)), xs, marker=marker, c=c, s=s,
75
label=label, alpha=alpha)
76
if connect:
77
plt.plot(range(len(xs)), xs, lw=1, c=c, alpha=alpha)'''
78
79
if ys is not None:
80
plt.plot(xs, ys, color=color, lw=lw, ls='--', label=label, **kwargs)
81
else:
82
plt.plot(xs, color=color, lw=lw, ls='--', label=label, **kwargs)
83
84
85
86
def plot_residual_limits(Ps, stds=1.):
87
""" plots standand deviation given in Ps as a yellow shaded region. One std
88
by default, use stds for a different choice (e.g. stds=3 for 3 standard
89
deviations.
90
"""
91
92
std = np.sqrt(Ps) * stds
93
94
plt.plot(-std, color='k', ls=':', lw=2)
95
plt.plot(std, color='k', ls=':', lw=2)
96
plt.fill_between(range(len(std)), -std, std,
97
facecolor='#ffff00', alpha=0.3)
98
99
100
def plot_track(xs, ys=None, label='Track', c='k', lw=2, **kwargs):
101
if ys is not None:
102
plt.plot(xs, ys, color=c, lw=lw, ls=':', label=label, **kwargs)
103
else:
104
plt.plot(xs, color=c, lw=lw, ls=':', label=label, **kwargs)
105
106
107
def plot_filter(xs, ys=None, c='#013afe', label='Filter', vars=None, **kwargs):
108
#def plot_filter(xs, ys=None, c='#6d904f', label='Filter', vars=None, **kwargs):
109
110
if ys is None:
111
ys = xs
112
xs = range(len(ys))
113
114
plt.plot(xs, ys, color=c, label=label, **kwargs)
115
116
if vars is None:
117
return
118
vars = np.asarray(vars)
119
120
std = np.sqrt(vars)
121
std_top = ys+std
122
std_btm = ys-std
123
124
plt.plot(xs, ys+std, linestyle=':', color='k', lw=2)
125
plt.plot(xs, ys-std, linestyle=':', color='k', lw=2)
126
plt.fill_between(xs, std_btm, std_top,
127
facecolor='yellow', alpha=0.2)
128
129
130
131
132
def _blob(x, y, area, colour):
133
"""
134
Draws a square-shaped blob with the given area (< 1) at
135
the given coordinates.
136
"""
137
hs = np.sqrt(area) / 2
138
xcorners = np.array([x - hs, x + hs, x + hs, x - hs])
139
ycorners = np.array([y - hs, y - hs, y + hs, y + hs])
140
plt.fill(xcorners, ycorners, colour, edgecolor=colour)
141
142
def hinton(W, maxweight=None):
143
"""
144
Draws a Hinton diagram for visualizing a weight matrix.
145
Temporarily disables matplotlib interactive mode if it is on,
146
otherwise this takes forever.
147
"""
148
reenable = False
149
if plt.isinteractive():
150
plt.ioff()
151
152
plt.clf()
153
height, width = W.shape
154
if not maxweight:
155
maxweight = 2**np.ceil(np.log(np.max(np.abs(W)))/np.log(2))
156
157
plt.fill(np.array([0, width, width, 0]),
158
np.array([0, 0, height, height]),
159
'gray')
160
161
plt.axis('off')
162
plt.axis('equal')
163
for x in range(width):
164
for y in range(height):
165
_x = x+1
166
_y = y+1
167
w = W[y, x]
168
if w > 0:
169
_blob(_x - 0.5,
170
height - _y + 0.5,
171
min(1, w/maxweight),
172
'white')
173
elif w < 0:
174
_blob(_x - 0.5,
175
height - _y + 0.5,
176
min(1, -w/maxweight),
177
'black')
178
if reenable:
179
plt.ion()
180
181
182
if __name__ == "__main__":
183
p = [0.2245871, 0.06288015, 0.06109133, 0.0581008, 0.09334062, 0.2245871,
184
0.06288015, 0.06109133, 0.0581008, 0.09334062]*2
185
bar_plot(p)
186
plot_measurements(p)
187