<s> import matplotlib as mpl <EOL> import matplotlib . pyplot as plt <EOL> from matplotlib . path import Path <EOL> from matplotlib . patches import PathPatch <EOL> import numpy as np <EOL> try : <EOL> numeric_types = ( int , float , long ) <EOL> except NameError : <EOL> numeric_types = ( int , float ) <EOL> class SimpleVectorPlotter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , interactive , ticks = False , figsize = None , limits = None ) : <EOL> """<STR_LIT>""" <EOL> plt . figure ( num = <NUM_LIT:1> , figsize = figsize ) <EOL> self . interactive = interactive <EOL> self . ticks = ticks <EOL> if interactive : <EOL> plt . ion ( ) <EOL> else : <EOL> plt . ioff ( ) <EOL> if limits is not None : <EOL> self . set_limits ( * limits ) <EOL> if not ticks : <EOL> self . no_ticks ( ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> self . _graphics = { } <EOL> self . _init_colors ( ) <EOL> def adjust_markers ( self ) : <EOL> figsize = plt . gcf ( ) . get_size_inches ( ) <EOL> r = min ( figsize [ <NUM_LIT:0> ] / <NUM_LIT:8> , figsize [ <NUM_LIT:1> ] / <NUM_LIT:6> ) <EOL> mpl . rcParams [ '<STR_LIT>' ] = <NUM_LIT:6> * r <EOL> mpl . rcParams [ '<STR_LIT>' ] = <NUM_LIT:0.5> * r <EOL> mpl . rcParams [ '<STR_LIT>' ] = r <EOL> mpl . rcParams [ '<STR_LIT>' ] = r <EOL> def adjust_markersize ( self , size ) : <EOL> figsize = plt . gcf ( ) . get_size_inches ( ) <EOL> r = min ( figsize [ <NUM_LIT:0> ] / <NUM_LIT:8> , figsize [ <NUM_LIT:1> ] / <NUM_LIT:6> ) <EOL> return <NUM_LIT:6> * r <EOL> def axis_on ( self , on ) : <EOL> """<STR_LIT>""" <EOL> if on : <EOL> plt . axis ( '<STR_LIT>' ) <EOL> else : <EOL> plt . axis ( '<STR_LIT>' ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> plt . cla ( ) <EOL> self . _graphics = { } <EOL> if not self . ticks : <EOL> self . no_ticks ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clear ( ) <EOL> plt . close ( ) <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> plt . show ( ) <EOL> def hide ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> graphics = self . _graphics [ name ] <EOL> graphic_type = type ( graphics [ <NUM_LIT:0> ] ) <EOL> if graphic_type is mpl . lines . Line2D : <EOL> for graphic in graphics : <EOL> plt . axes ( ) . lines . remove ( graphic ) <EOL> elif graphic_type is mpl . patches . Polygon or graphic_type is mpl . patches . PathPatch : <EOL> for graphic in graphics : <EOL> plt . axes ( ) . patches . remove ( graphic ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( graphic_type ) ) <EOL> except ( KeyError , ValueError ) : <EOL> pass <EOL> def plot_line ( self , data , symbol = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> graphics = self . _plot_line ( data , symbol , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , symbol or kwargs ) <EOL> def plot_multiline ( self , data , symbol = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_symbol = symbol or kwargs <EOL> symbol = symbol or self . _line_symbol ( ) <EOL> graphics = self . _plot_multiline ( data , symbol , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , has_symbol ) <EOL> def plot_multipoint ( self , data , symbol = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_symbol = symbol or kwargs <EOL> symbol = symbol or self . _point_symbol ( ) <EOL> graphics = self . _plot_multipoint ( data , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , has_symbol ) <EOL> def plot_multipolygon ( self , data , color = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_symbol = bool ( color or kwargs ) <EOL> if not ( '<STR_LIT>' in kwargs or '<STR_LIT>' in kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = color or self . _next_color ( ) <EOL> graphics = self . _plot_multipolygon ( data , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , has_symbol ) <EOL> def plot_point ( self , data , symbol = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_symbol = symbol or kwargs <EOL> symbol = symbol or self . _point_symbol ( ) <EOL> graphics = self . _plot_point ( data , symbol , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , has_symbol ) <EOL> def plot_polygon ( self , data , color = '<STR_LIT>' , name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_symbol = bool ( color or kwargs ) <EOL> if not ( '<STR_LIT>' in kwargs or '<STR_LIT>' in kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = color or self . _next_color ( ) <EOL> graphics = self . _plot_polygon ( data , ** kwargs ) <EOL> self . _set_graphics ( graphics , name , has_symbol ) <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . hide ( name ) <EOL> del self . _graphics [ name ] <EOL> except KeyError : <EOL> pass <EOL> def save ( self , fn , dpi = <NUM_LIT> ) : <EOL> plt . savefig ( fn , dpi = dpi , bbox_inches = '<STR_LIT>' , pad_inches = <NUM_LIT> ) <EOL> def set_limits ( self , x_min , x_max , y_min , y_max ) : <EOL> """<STR_LIT>""" <EOL> self . x_lim = x_min , x_max <EOL> self . y_lim = y_min , y_max <EOL> self . _set_limits ( ) <EOL> def show ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> graphics = self . _graphics [ name ] <EOL> graphic_type = type ( graphics [ <NUM_LIT:0> ] ) <EOL> if graphic_type is mpl . lines . Line2D : <EOL> for graphic in graphics : <EOL> plt . axes ( ) . add_line ( graphic ) <EOL> elif graphic_type is mpl . patches . Polygon or graphic_type is mpl . patches . PathPatch : <EOL> for graphic in graphics : <EOL> plt . axes ( ) . add_patch ( graphic ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( graphic_type ) ) <EOL> except KeyError : <EOL> pass <EOL> def no_ticks ( self ) : <EOL> plt . gca ( ) . get_xaxis ( ) . set_ticks ( [ ] ) <EOL> plt . gca ( ) . get_yaxis ( ) . set_ticks ( [ ] ) <EOL> def zoom ( self , factor ) : <EOL> """<STR_LIT>""" <EOL> x_min , x_max , y_min , y_max = plt . axis ( ) <EOL> x_delta = ( x_max - x_min ) * factor / <NUM_LIT:100> <EOL> y_delta = ( y_max - y_min ) * factor / <NUM_LIT:100> <EOL> plt . axis ( ( x_min + x_delta , x_max - x_delta , <EOL> y_min + y_delta , y_max - y_delta ) ) <EOL> def _clockwise ( self , data ) : <EOL> """<STR_LIT>""" <EOL> total = <NUM_LIT:0> <EOL> x1 , y1 = data [ <NUM_LIT:0> ] <EOL> for x , y in data [ <NUM_LIT:1> : ] : <EOL> total += ( x - x1 ) * ( y + y1 ) <EOL> x1 , y1 = x , y <EOL> x , y = data [ <NUM_LIT:0> ] <EOL> total += ( x - x1 ) * ( y + y1 ) <EOL> return total > <NUM_LIT:0> <EOL> def _codes ( self , data ) : <EOL> """<STR_LIT>""" <EOL> codes = np . ones ( len ( data ) , dtype = np . int ) * Path . LINETO <EOL> codes [ <NUM_LIT:0> ] = Path . MOVETO <EOL> return codes <EOL> def _init_colors ( self ) : <EOL> if mpl . __version__ >= '<STR_LIT>' : <EOL> self . colors = list ( mpl . rcParams [ '<STR_LIT>' ] ) <EOL> self . current_color = - <NUM_LIT:1> <EOL> self . _next_color = self . _next_color_new <EOL> else : <EOL> self . _next_color = self . _next_color_old <EOL> def _line_symbol ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _next_color ( ) + '<STR_LIT:->' <EOL> def _next_color_old ( self ) : <EOL> """<STR_LIT>""" <EOL> return next ( plt . gca ( ) . _get_lines . color_cycle ) <EOL> def _next_color_new ( self ) : <EOL> """<STR_LIT>""" <EOL> self . current_color += <NUM_LIT:1> <EOL> if self . current_color >= len ( self . colors ) : <EOL> self . current_color = <NUM_LIT:0> <EOL> return self . colors [ self . current_color ] [ '<STR_LIT>' ] <EOL> def _order_vertices ( self , data , clockwise = True ) : <EOL> """<STR_LIT>""" <EOL> self . _clockwise ( data ) != clockwise or data . reverse ( ) <EOL> if data [ <NUM_LIT:0> ] != data [ - <NUM_LIT:1> ] : <EOL> data . append ( data [ <NUM_LIT:0> ] ) <EOL> return data <EOL> def _plot_line ( self , data , symbol , ** kwargs ) : <EOL> x , y = zip ( * data ) <EOL> return plt . plot ( x , y , symbol , ** kwargs ) <EOL> def _plot_multiline ( self , data , symbol , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> graphics = [ ] <EOL> for line in data : <EOL> graphics += self . _plot_line ( line , symbol , ** kwargs ) <EOL> return graphics <EOL> def _plot_multipoint ( self , data , symbol , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> graphics = [ ] <EOL> for point in data : <EOL> graphics += self . _plot_point ( point , symbol , ** kwargs ) <EOL> return graphics <EOL> def _plot_multipolygon ( self , data , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> graphics = [ ] <EOL> for poly in data : <EOL> graphics += self . _plot_polygon ( poly , ** kwargs ) <EOL> return graphics <EOL> def _plot_point ( self , data , symbol , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return plt . plot ( data [ <NUM_LIT:0> ] , data [ <NUM_LIT:1> ] , symbol , ** kwargs ) <EOL> def _plot_polygon ( self , data , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> outer = self . _order_vertices ( data [ <NUM_LIT:0> ] , True ) <EOL> inner = [ self . _order_vertices ( d , False ) for d in data [ <NUM_LIT:1> : ] ] <EOL> vertices = np . concatenate ( <EOL> [ np . asarray ( outer ) ] + [ np . asarray ( i ) for i in inner ] ) <EOL> codes = np . concatenate ( <EOL> [ self . _codes ( outer ) ] + [ self . _codes ( i ) for i in inner ] ) <EOL> patch = PathPatch ( Path ( vertices , codes ) , ** kwargs ) <EOL> plt . axes ( ) . add_patch ( patch ) <EOL> return [ patch ] <EOL> def _point_symbol ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _next_color ( ) + '<STR_LIT:o>' <EOL> def _same_type ( self , graphic1 , graphic2 ) : <EOL> """<STR_LIT>""" <EOL> if type ( graphic1 ) is not type ( graphic2 ) : <EOL> return False <EOL> if type ( graphic1 ) is mpl . patches . Polygon : <EOL> return True <EOL> if len ( graphic1 . get_xdata ( ) ) == len ( graphic2 . get_xdata ( ) ) : <EOL> return True <EOL> return len ( graphic1 . get_xdata ( ) ) > <NUM_LIT:1> and len ( graphic2 . get_xdata ( ) ) > <NUM_LIT:1> <EOL> def _set_graphics ( self , graphics , name , has_symbol ) : <EOL> """<STR_LIT>""" <EOL> name = name or len ( self . _graphics ) <EOL> if name in self . _graphics : <EOL> self . hide ( name ) <EOL> if not has_symbol and self . _same_type ( graphics [ <NUM_LIT:0> ] , self . _graphics [ name ] [ <NUM_LIT:0> ] ) : <EOL> styled_graphic = self . _graphics [ name ] [ <NUM_LIT:0> ] <EOL> for graphic in graphics : <EOL> graphic . update_from ( styled_graphic ) <EOL> self . _graphics [ name ] = graphics <EOL> plt . axis ( '<STR_LIT>' ) <EOL> def _set_limits ( self ) : <EOL> """<STR_LIT>""" <EOL> plt . xlim ( * self . x_lim ) <EOL> plt . ylim ( * self . y_lim ) <EOL> plt . axes ( ) . set_aspect ( '<STR_LIT>' ) </s>
