<s> "<STR_LIT>" <EOL> from __future__ import absolute_import <EOL> import itertools <EOL> import warnings <EOL> import matplotlib as mpl <EOL> import numpy as np <EOL> from six import string_types <EOL> from ... models import ( ColumnDataSource , FactorRange , DataRange1d , DatetimeAxis , GlyphRenderer , <EOL> Grid , GridPlot , LinearAxis , Plot , CategoricalAxis , Legend ) <EOL> from ... models . glyphs import ( Asterisk , Circle , Cross , Diamond , InvertedTriangle , <EOL> Line , MultiLine , Patches , Square , Text , Triangle , X ) <EOL> from ... plotting import DEFAULT_TOOLS <EOL> from ... plotting . helpers import _process_tools_arg <EOL> from ... util . dependencies import import_optional <EOL> from . mplexporter . renderers import Renderer <EOL> from . mpl_helpers import convert_color , convert_dashes , get_props_cycled , xkcd_line <EOL> pd = import_optional ( '<STR_LIT>' ) <EOL> class BokehRenderer ( Renderer ) : <EOL> def __init__ ( self , use_pd , xkcd ) : <EOL> "<STR_LIT>" <EOL> self . fig = None <EOL> self . use_pd = use_pd <EOL> self . xkcd = xkcd <EOL> self . zorder = { } <EOL> self . handles = { } <EOL> def open_figure ( self , fig , props ) : <EOL> "<STR_LIT>" <EOL> self . width = int ( props [ '<STR_LIT>' ] * props [ '<STR_LIT>' ] ) <EOL> self . height = int ( props [ '<STR_LIT>' ] * props [ '<STR_LIT>' ] ) <EOL> self . plot = Plot ( x_range = DataRange1d ( ) , <EOL> y_range = DataRange1d ( ) , <EOL> plot_width = self . width , <EOL> plot_height = self . height ) <EOL> def close_figure ( self , fig ) : <EOL> "<STR_LIT>" <EOL> tool_objs = _process_tools_arg ( self . plot , DEFAULT_TOOLS ) <EOL> self . plot . add_tools ( * tool_objs ) <EOL> if len ( fig . axes ) <= <NUM_LIT:1> : <EOL> self . fig = self . plot <EOL> self . plot . renderers . sort ( key = lambda x : self . zorder . get ( x . _id , <NUM_LIT:0> ) ) <EOL> else : <EOL> subrends = [ ] <EOL> for i in range ( <NUM_LIT:1> , len ( self . _axes ) ) : <EOL> start , end = self . _axes [ i - <NUM_LIT:1> ] , self . _axes [ i ] <EOL> subrends += [ self . plot . renderers [ start : end ] ] <EOL> plots = [ ] <EOL> for i , axes in enumerate ( fig . axes ) : <EOL> _plot = Plot ( x_range = self . plot . x_range , <EOL> y_range = self . plot . y_range , <EOL> plot_width = self . width , <EOL> plot_height = self . height ) <EOL> _plot . title = "<STR_LIT>" <EOL> _tool_objs = _process_tools_arg ( _plot , DEFAULT_TOOLS ) <EOL> _plot . add_tools ( * _tool_objs ) <EOL> _plot_rends = subrends [ i ] <EOL> for r in _plot_rends : <EOL> if not isinstance ( r , GlyphRenderer ) : <EOL> r . plot = None <EOL> for r in _plot_rends : <EOL> if isinstance ( r , GlyphRenderer ) : <EOL> _plot . renderers . append ( r ) <EOL> elif isinstance ( r , Grid ) : <EOL> _plot . add_layout ( r ) <EOL> else : <EOL> if r in self . plot . below : <EOL> _plot . add_layout ( r , '<STR_LIT>' ) <EOL> elif r in self . plot . above : <EOL> _plot . add_layout ( r , '<STR_LIT>' ) <EOL> elif r in self . plot . left : <EOL> _plot . add_layout ( r , '<STR_LIT:left>' ) <EOL> elif r in self . plot . right : <EOL> _plot . add_layout ( r , '<STR_LIT:right>' ) <EOL> _plot . renderers . sort ( key = lambda x : self . zorder . get ( x . _id , <NUM_LIT:0> ) ) <EOL> plots . append ( _plot ) <EOL> ( a , b , c ) = fig . axes [ <NUM_LIT:0> ] . get_geometry ( ) <EOL> p = np . array ( plots ) <EOL> n = np . resize ( p , ( a , b ) ) <EOL> grid = GridPlot ( children = n . tolist ( ) ) <EOL> self . fig = grid <EOL> def open_axes ( self , ax , props ) : <EOL> "<STR_LIT>" <EOL> self . ax = ax <EOL> self . plot . title = ax . get_title ( ) <EOL> if props . get ( '<STR_LIT>' , False ) : <EOL> props [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = props [ '<STR_LIT>' ] <EOL> if props . get ( '<STR_LIT>' , False ) : <EOL> props [ '<STR_LIT>' ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] = props [ '<STR_LIT>' ] <EOL> for props in props [ '<STR_LIT>' ] : <EOL> if props [ '<STR_LIT>' ] == "<STR_LIT>" : location , dim , thing = "<STR_LIT>" , <NUM_LIT:0> , ax . xaxis <EOL> elif props [ '<STR_LIT>' ] == "<STR_LIT>" : location , dim , thing = "<STR_LIT>" , <NUM_LIT:0> , ax . xaxis <EOL> else : location , dim , thing = props [ '<STR_LIT>' ] , <NUM_LIT:1> , ax . yaxis <EOL> baxis = self . make_axis ( thing , location , props ) <EOL> if dim == <NUM_LIT:0> : <EOL> gridlines = ax . get_xgridlines ( ) <EOL> else : <EOL> gridlines = ax . get_ygridlines ( ) <EOL> if gridlines : <EOL> self . make_grid ( baxis , dim , gridlines [ <NUM_LIT:0> ] ) <EOL> def close_axes ( self , ax ) : <EOL> "<STR_LIT>" <EOL> background_fill_color = convert_color ( ax . get_axis_bgcolor ( ) ) <EOL> self . plot . background_fill_color = background_fill_color <EOL> if self . xkcd : <EOL> self . plot . title_text_font = "<STR_LIT>" <EOL> self . plot . title_text_font_style = "<STR_LIT>" <EOL> self . plot . title_text_color = "<STR_LIT>" <EOL> self . _axes = getattr ( self , "<STR_LIT>" , [ <NUM_LIT:0> ] ) <EOL> self . _axes . append ( len ( self . plot . renderers ) ) <EOL> def open_legend ( self , legend , props ) : <EOL> lgnd = Legend ( location = "<STR_LIT>" ) <EOL> try : <EOL> for label , obj in zip ( props [ '<STR_LIT>' ] , props [ '<STR_LIT>' ] ) : <EOL> lgnd . legends . append ( ( label , [ self . handles [ id ( obj ) ] ] ) ) <EOL> self . plot . add_layout ( lgnd ) <EOL> except KeyError : <EOL> pass <EOL> def close_legend ( self , legend ) : <EOL> pass <EOL> def draw_line ( self , data , coordinates , style , label , mplobj = None ) : <EOL> "<STR_LIT>" <EOL> _x = data [ : , <NUM_LIT:0> ] <EOL> if pd and self . use_pd : <EOL> try : <EOL> x = [ pd . Period ( ordinal = int ( i ) , freq = self . ax . xaxis . freq ) . to_timestamp ( ) for i in _x ] <EOL> except AttributeError : <EOL> x = _x <EOL> else : <EOL> x = _x <EOL> y = data [ : , <NUM_LIT:1> ] <EOL> if self . xkcd : <EOL> x , y = xkcd_line ( x , y ) <EOL> line = Line ( ) <EOL> source = ColumnDataSource ( ) <EOL> line . x = source . add ( x ) <EOL> line . y = source . add ( y ) <EOL> line . line_color = convert_color ( style [ '<STR_LIT>' ] ) <EOL> line . line_width = style [ '<STR_LIT>' ] <EOL> line . line_alpha = style [ '<STR_LIT>' ] <EOL> line . line_dash = [ ] if style [ '<STR_LIT>' ] is "<STR_LIT:none>" else [ int ( i ) for i in style [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) ] <EOL> if self . xkcd : <EOL> line . line_width = <NUM_LIT:3> <EOL> r = self . plot . add_glyph ( source , line ) <EOL> self . zorder [ r . _id ] = style [ '<STR_LIT>' ] <EOL> self . handles [ id ( mplobj ) ] = r <EOL> def draw_markers ( self , data , coordinates , style , label , mplobj = None ) : <EOL> "<STR_LIT>" <EOL> x = data [ : , <NUM_LIT:0> ] <EOL> y = data [ : , <NUM_LIT:1> ] <EOL> marker_map = { <EOL> "<STR_LIT:o>" : Circle , <EOL> "<STR_LIT:s>" : Square , <EOL> "<STR_LIT:+>" : Cross , <EOL> "<STR_LIT>" : Triangle , <EOL> "<STR_LIT:v>" : InvertedTriangle , <EOL> "<STR_LIT:x>" : X , <EOL> "<STR_LIT:d>" : Diamond , <EOL> "<STR_LIT:D>" : Diamond , <EOL> "<STR_LIT:*>" : Asterisk , <EOL> } <EOL> try : <EOL> marker = marker_map [ style [ '<STR_LIT>' ] ] ( ) <EOL> except KeyError : <EOL> warnings . warn ( "<STR_LIT>" % style [ '<STR_LIT>' ] ) <EOL> marker = Circle ( ) <EOL> source = ColumnDataSource ( ) <EOL> marker . x = source . add ( x ) <EOL> marker . y = source . add ( y ) <EOL> marker . line_color = convert_color ( style [ '<STR_LIT>' ] ) <EOL> marker . fill_color = convert_color ( style [ '<STR_LIT>' ] ) <EOL> marker . line_width = style [ '<STR_LIT>' ] <EOL> marker . size = style [ '<STR_LIT>' ] <EOL> marker . fill_alpha = marker . line_alpha = style [ '<STR_LIT>' ] <EOL> r = self . plot . add_glyph ( source , marker ) <EOL> self . zorder [ r . _id ] = style [ '<STR_LIT>' ] <EOL> self . handles [ id ( mplobj ) ] = r <EOL> def draw_path ( self , data , coordinates , pathcodes , style , <EOL> offset = None , offset_coordinates = "<STR_LIT:data>" , mplobj = None ) : <EOL> pass <EOL> def draw_text ( self , text , position , coordinates , style , <EOL> text_type = None , mplobj = None ) : <EOL> "<STR_LIT>" <EOL> if text_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:title>' ] : <EOL> return <EOL> if coordinates != '<STR_LIT:data>' : <EOL> return <EOL> x , y = position <EOL> text = Text ( x = x , y = y , text = [ text ] ) <EOL> alignment_map = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> text . text_alpha = style [ '<STR_LIT>' ] <EOL> text . text_font_size = "<STR_LIT>" % style [ '<STR_LIT>' ] <EOL> text . text_color = convert_color ( style [ '<STR_LIT>' ] ) <EOL> text . text_align = style [ '<STR_LIT>' ] <EOL> text . text_baseline = alignment_map [ style [ '<STR_LIT>' ] ] <EOL> text . angle = style [ '<STR_LIT>' ] <EOL> source = ColumnDataSource ( ) <EOL> r = self . plot . add_glyph ( source , text ) <EOL> self . zorder [ r . _id ] = style [ '<STR_LIT>' ] <EOL> self . handles [ id ( mplobj ) ] = r <EOL> def draw_image ( self , imdata , extent , coordinates , style , mplobj = None ) : <EOL> pass <EOL> def make_axis ( self , ax , location , props ) : <EOL> "<STR_LIT>" <EOL> tf = props [ '<STR_LIT>' ] <EOL> tv = props [ '<STR_LIT>' ] <EOL> if tf and any ( isinstance ( x , string_types ) for x in tf ) : <EOL> laxis = CategoricalAxis ( axis_label = ax . get_label_text ( ) ) <EOL> assert np . min ( tv ) >= <NUM_LIT:0> , "<STR_LIT>" <EOL> offset = np . min ( tv ) - <NUM_LIT:1> <EOL> rng = FactorRange ( factors = [ str ( x ) for x in tf ] , offset = offset ) <EOL> if location in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . plot . x_range = rng <EOL> else : <EOL> self . plot . y_range = rng <EOL> else : <EOL> if props [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> laxis = LinearAxis ( axis_label = ax . get_label_text ( ) ) <EOL> elif props [ '<STR_LIT>' ] == "<STR_LIT:date>" : <EOL> laxis = DatetimeAxis ( axis_label = ax . get_label_text ( ) ) <EOL> self . plot . add_layout ( laxis , location ) <EOL> label = ax . get_label ( ) <EOL> self . text_props ( label , laxis , prefix = "<STR_LIT>" ) <EOL> if props [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> laxis . major_tick_line_color = None <EOL> laxis . minor_tick_line_color = None <EOL> laxis . major_label_text_color = None <EOL> ticklabels = ax . get_ticklabels ( ) <EOL> if ticklabels : <EOL> self . text_props ( ticklabels [ <NUM_LIT:0> ] , laxis , prefix = "<STR_LIT>" ) <EOL> if self . xkcd : <EOL> laxis . axis_line_width = <NUM_LIT:3> <EOL> laxis . axis_label_text_font = "<STR_LIT>" <EOL> laxis . axis_label_text_font_style = "<STR_LIT>" <EOL> laxis . axis_label_text_color = "<STR_LIT>" <EOL> laxis . major_label_text_font = "<STR_LIT>" <EOL> laxis . major_label_text_font_style = "<STR_LIT>" <EOL> laxis . major_label_text_color = "<STR_LIT>" <EOL> return laxis <EOL> def make_grid ( self , baxis , dimension , gridline ) : <EOL> "<STR_LIT>" <EOL> lgrid = Grid ( dimension = dimension , <EOL> ticker = baxis . ticker , <EOL> grid_line_color = convert_color ( gridline . get_color ( ) ) , <EOL> grid_line_width = gridline . get_linewidth ( ) ) <EOL> self . plot . add_layout ( lgrid ) <EOL> def make_line_collection ( self , col ) : <EOL> "<STR_LIT>" <EOL> xydata = col . get_segments ( ) <EOL> t_xydata = [ np . transpose ( seg ) for seg in xydata ] <EOL> xs = [ t_xydata [ x ] [ <NUM_LIT:0> ] for x in range ( len ( t_xydata ) ) ] <EOL> ys = [ t_xydata [ x ] [ <NUM_LIT:1> ] for x in range ( len ( t_xydata ) ) ] <EOL> if self . xkcd : <EOL> xkcd_xs = [ xkcd_line ( xs [ i ] , ys [ i ] ) [ <NUM_LIT:0> ] for i in range ( len ( xs ) ) ] <EOL> xkcd_ys = [ xkcd_line ( xs [ i ] , ys [ i ] ) [ <NUM_LIT:1> ] for i in range ( len ( ys ) ) ] <EOL> xs = xkcd_xs <EOL> ys = xkcd_ys <EOL> multiline = MultiLine ( ) <EOL> source = ColumnDataSource ( ) <EOL> multiline . xs = source . add ( xs ) <EOL> multiline . ys = source . add ( ys ) <EOL> self . multiline_props ( source , multiline , col ) <EOL> r = self . plot . add_glyph ( source , multiline ) <EOL> self . zorder [ r . _id ] = col . zorder <EOL> self . handles [ id ( col ) ] = r <EOL> def make_poly_collection ( self , col ) : <EOL> "<STR_LIT>" <EOL> xs = [ ] <EOL> ys = [ ] <EOL> for path in col . get_paths ( ) : <EOL> for sub_poly in path . to_polygons ( ) : <EOL> xx , yy = sub_poly . transpose ( ) <EOL> xs . append ( xx ) <EOL> ys . append ( yy ) <EOL> patches = Patches ( ) <EOL> source = ColumnDataSource ( ) <EOL> patches . xs = source . add ( xs ) <EOL> patches . ys = source . add ( ys ) <EOL> self . patches_props ( source , patches , col ) <EOL> r = self . plot . add_glyph ( source , patches ) <EOL> self . zorder [ r . _id ] = col . zorder <EOL> self . handles [ id ( col ) ] = r <EOL> def multiline_props ( self , source , multiline , col ) : <EOL> "<STR_LIT>" <EOL> colors = get_props_cycled ( col , col . get_colors ( ) , fx = lambda x : mpl . colors . rgb2hex ( x ) ) <EOL> colors = [ convert_color ( x ) for x in colors ] <EOL> widths = get_props_cycled ( col , col . get_linewidth ( ) ) <EOL> multiline . line_color = source . add ( colors ) <EOL> multiline . line_width = source . add ( widths ) <EOL> if col . get_alpha ( ) is not None : <EOL> multiline . line_alpha = col . get_alpha ( ) <EOL> offset = col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if not col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> on_off = [ ] <EOL> else : <EOL> on_off = map ( int , col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> multiline . line_dash_offset = convert_dashes ( offset ) <EOL> multiline . line_dash = list ( convert_dashes ( tuple ( on_off ) ) ) <EOL> def patches_props ( self , source , patches , col ) : <EOL> "<STR_LIT>" <EOL> face_colors = get_props_cycled ( col , col . get_facecolors ( ) , fx = lambda x : mpl . colors . rgb2hex ( x ) ) <EOL> face_colors = [ convert_color ( x ) for x in face_colors ] <EOL> patches . fill_color = source . add ( face_colors ) <EOL> edge_colors = get_props_cycled ( col , col . get_edgecolors ( ) , fx = lambda x : mpl . colors . rgb2hex ( x ) ) <EOL> edge_colors = [ convert_color ( x ) for x in edge_colors ] <EOL> patches . line_color = source . add ( edge_colors ) <EOL> widths = get_props_cycled ( col , col . get_linewidth ( ) ) <EOL> patches . line_width = source . add ( widths ) <EOL> if col . get_alpha ( ) is not None : <EOL> patches . line_alpha = col . get_alpha ( ) <EOL> patches . fill_alpha = col . get_alpha ( ) <EOL> offset = col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if not col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> on_off = [ ] <EOL> else : <EOL> on_off = map ( int , col . get_linestyle ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> patches . line_dash_offset = convert_dashes ( offset ) <EOL> patches . line_dash = list ( convert_dashes ( tuple ( on_off ) ) ) <EOL> def text_props ( self , text , obj , prefix = "<STR_LIT>" ) : <EOL> fp = text . get_font_properties ( ) <EOL> setattr ( obj , prefix + "<STR_LIT>" , fp . get_family ( ) [ <NUM_LIT:0> ] ) <EOL> setattr ( obj , prefix + "<STR_LIT>" , "<STR_LIT>" % fp . get_size_in_points ( ) ) <EOL> setattr ( obj , prefix + "<STR_LIT>" , fp . get_style ( ) ) </s>
