00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import matplotlib.pyplot as pp
00032 import math, numpy as np
00033 from matplotlib.patches import Ellipse
00034
00035
00036
00037
00038
00039 def figure(fig_num=None, dpi=None):
00040 return pp.figure(fig_num, dpi=dpi, facecolor='w')
00041
00042
00043
00044
00045
00046 def legend(loc='best',display_mode='normal', draw_frame = True,
00047 handlelength=0.003):
00048 params = {'legend.fontsize': 10}
00049 pp.rcParams.update(params)
00050 if display_mode == 'normal':
00051 leg = pp.legend(loc=loc)
00052 leg.draw_frame(draw_frame)
00053 elif display_mode == 'less_space':
00054 leg = pp.legend(loc=loc,handletextpad=0.7,handlelength=handlelength,labelspacing=0.01,
00055 markerscale=0.5)
00056 leg.draw_frame(draw_frame)
00057
00058
00059
00060
00061 def random_color():
00062 r = '%02X'%np.random.randint(0, 255)
00063 g = '%02X'%np.random.randint(0, 255)
00064 b = '%02X'%np.random.randint(0, 255)
00065 c = '#' + r + g + b
00066 return c
00067
00068
00069
00070
00071 def set_figure_size(fig_width, fig_height):
00072 inches_per_cm = 1./2.54
00073 fig_width = fig_width * inches_per_cm
00074 fig_height = fig_height * inches_per_cm
00075 fig_size = [fig_width, fig_height]
00076 params = {'backend': 'WXAgg',
00077 'axes.labelsize': 12,
00078 'text.fontsize': 12,
00079 'legend.fontsize': 12,
00080 'xtick.labelsize': 10,
00081 'ytick.labelsize': 10,
00082 'text.usetex': True,
00083 'figure.figsize': fig_size}
00084 pp.rcParams.update(params)
00085
00086 def reduce_figure_margins(left=0.1, right=0.98, top=0.99, bottom=0.15):
00087 f = pp.gcf()
00088 f.subplots_adjust(bottom=bottom, top=top, right=right, left=left)
00089
00090
00091
00092 def flip_x_axis(ax):
00093 ax.set_xlim(ax.get_xlim()[::-1])
00094
00095
00096 def flip_y_axis(ax):
00097 ax.set_ylim(ax.get_ylim()[::-1])
00098
00099
00100
00101
00102
00103 def plot_ellipse_cov(pos, P, edge_color, face_color='w', alpha=1.):
00104 U, s , Vh = np.linalg.svd(P)
00105 ang = math.atan2(U[1,0],U[0,0])
00106 w1 = 2.0*math.sqrt(s[0])
00107 w2 = 2.0*math.sqrt(s[1])
00108 return plot_ellipse(pos, ang, w1, w2, edge_color, face_color,
00109 alpha)
00110
00111
00112
00113
00114 def plot_ellipse(pos, angle, w1, w2, edge_color, face_color='w',
00115 alpha=1.):
00116 orient = math.degrees(angle)
00117 e = Ellipse(xy=pos, width=w1, height=w2, angle=orient,
00118 facecolor=face_color, edgecolor=edge_color)
00119 e.set_alpha(alpha)
00120 ax = pp.gca()
00121 ax.add_patch(e)
00122 return e
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 def plot_circle(cx, cy, rad, start_angle, end_angle, step=math.radians(2),
00136 color='k', label='', alpha=1.0, linewidth=2):
00137 if start_angle>end_angle:
00138 step = -step
00139
00140 n_step = int((end_angle-start_angle)/step+0.5)
00141 x,y=[],[]
00142 for i in range(n_step):
00143 x.append(cx-rad*math.sin(start_angle+i*step))
00144 y.append(cy+rad*math.cos(start_angle+i*step))
00145 x.append(cx-rad*math.sin(end_angle))
00146 y.append(cy+rad*math.cos(end_angle))
00147
00148 pp.axis('equal')
00149 return pp.plot(x,y,c=color,label=label,linewidth=linewidth, alpha=alpha)
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 def plot_rectangle(cx, cy, slope, width, length, color='k', label='',
00160 alpha=1.0, linewidth=2):
00161
00162
00163 mEdge = np.matrix([[-length, length, length, -length, -length],
00164 [width, width, -width, -width, width]]) * 0.5
00165
00166 mRot = np.matrix([[np.cos(slope), -np.sin(slope)],
00167 [np.sin(slope), np.cos(slope)]])
00168
00169 mRotEdge = mRot * mEdge
00170
00171 x,y=[],[]
00172
00173 for i in range(5):
00174 x.append(cx + mRotEdge[0,i])
00175 y.append(cy + mRotEdge[1,i])
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 pp.axis('equal')
00193 return pp.plot(x,y,c=color,label=label,linewidth=linewidth, alpha=alpha)
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 def plot_radii(cx, cy, rad, start_angle, end_angle, interval=math.radians(15),
00205 color='k', label='', alpha=1.0, linewidth=1.):
00206 if start_angle < 0.:
00207 start_angle = 2*math.pi+start_angle
00208 if end_angle < 0.:
00209 end_angle = 2*math.pi+end_angle
00210 if start_angle > end_angle:
00211 interval = -interval
00212
00213 n_step = int((end_angle-start_angle)/interval+0.5)
00214 x,y=[],[]
00215 for i in range(n_step):
00216 x.append(cx)
00217 y.append(cy)
00218 x.append(cx-rad*math.sin(start_angle+i*interval))
00219 y.append(cy+rad*(math.cos(start_angle+i*interval)))
00220 x.append(cx)
00221 y.append(cy)
00222 x.append(cx-rad*math.sin(end_angle))
00223 y.append(cy+rad*math.cos(end_angle))
00224
00225 pp.plot(x,y,c=color,label=label,linewidth=linewidth,alpha=alpha)
00226 pp.axis('equal')
00227
00228
00229
00230
00231
00232 def plot_histogram(left, height, width=0.8, label='',
00233 align='center', color='b', alpha=1.):
00234 pb_obj = pp.bar(left, height, width=width, align=align,
00235 color=color, alpha=alpha, label=label, linewidth=0)
00236 return pb_obj
00237
00238
00239
00240
00241