matplotlib_util.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 
00031 import matplotlib.pyplot as pp
00032 import math, numpy as np
00033 from matplotlib.patches import Ellipse
00034 
00035 
00036 ## calls pylab.figure() and returns the figure
00037 # other params can be added if required.
00038 # @param dpi - changing this can change the size of the font.
00039 def figure(fig_num=None, dpi=None):
00040     return pp.figure(fig_num, dpi=dpi, facecolor='w')
00041 
00042 
00043 ## legend drawing helper
00044 # @param loc - 'best', 'upper left' ...
00045 # @param display_mode - 'normal', 'less_space'
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 # generate a random color.
00060 # @return string of the form #xxxxxx
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 # @param figure width in cm
00070 # @param figure height in cm
00071 def set_figure_size(fig_width, fig_height):
00072     inches_per_cm = 1./2.54
00073     fig_width = fig_width * inches_per_cm     # width in inches
00074     fig_height = fig_height * inches_per_cm   # height in inches
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 ## typical usage: ax = pp.gca(); mpu.flip_x_axis(ax)
00092 def flip_x_axis(ax):
00093     ax.set_xlim(ax.get_xlim()[::-1])
00094 
00095 ## typical usage: ax = pp.gca(); mpu.flip_y_axis(ax)
00096 def flip_y_axis(ax):
00097     ax.set_ylim(ax.get_ylim()[::-1])
00098 
00099 
00100 ## plot an ellipse
00101 # @param mn - center of ellipe. (2x1 np matrix)
00102 # @param P - covariance matrix.
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 ## plot an ellipse
00112 # @param mn - center of ellipe. (2x1 np matrix)
00113 # @param angle of the ellipse (RADIANS)
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 ## plot circle (or an arc counterclockwise starting from the y-axis)
00125 # @param cx - x coord of center of circle.
00126 # @param cy - y coord of center of circle.
00127 # @param rad - radius of the circle
00128 # @param start_angle -  starting angle for the arcin RADIANS. (0 is y axis)
00129 # @param end_angle -  ending angle for the arcin RADIANS. (0 is y axis)
00130 # @param step - step size for the linear segments.
00131 # @param color - color of the circle.
00132 # @param label - legend label.
00133 #
00134 # circle plotted as bunch of linear segments. back to LOGO days.
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 ## plot rectangle 
00152 # @param cx - x coord of center of rectangle.
00153 # @param cy - y coord of center of rectangle.
00154 # @param slope - slope of rectangle. (0 is aligned along x axis)
00155 # @param width - width of the rectangle
00156 # @param length - length of the rectangle
00157 # @param color - color of the circle.
00158 # @param label - legend label.
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     ## x.append(cx-length/2.0)
00178     ## y.append(cy+width/2.0)
00179 
00180     ## x.append(cx+length/2.0)
00181     ## y.append(cy+width/2.0)
00182     
00183     ## x.append(cx+length/2.0)
00184     ## y.append(cy-width/2.0)
00185     
00186     ## x.append(cx-length/2.0)
00187     ## y.append(cy-width/2.0)
00188 
00189     ## x.append(cx-length/2.0)
00190     ## y.append(cy+width/2.0)
00191 
00192     pp.axis('equal')
00193     return pp.plot(x,y,c=color,label=label,linewidth=linewidth, alpha=alpha)
00194     
00195 ## plot radii at regular angular intervals.
00196 # @param cx - x coord of center of circle.
00197 # @param cy - y coord of center of circle.
00198 # @param rad - radius of the circle
00199 # @param start_angle -  starting angle for the arcin RADIANS. (0 is y axis)
00200 # @param end_angle -  ending angle for the arcin RADIANS. (0 is y axis)
00201 # @param interval - angular intervals for the radii
00202 # @param color - color of the circle.
00203 # @param label - legend label.
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 ## plot a histogram.
00230 # @param left - left edge of the bin. (array-like)
00231 # @param height - height of each bin (array-like)
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 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06