<s> import sys <EOL> from io import BytesIO <EOL> from tempfile import NamedTemporaryFile <EOL> from contextlib import contextmanager <EOL> from itertools import chain <EOL> import matplotlib as mpl <EOL> from matplotlib import pyplot as plt <EOL> from matplotlib . transforms import Bbox , TransformedBbox , Affine2D <EOL> from mpl_toolkits . mplot3d import Axes3D <EOL> import param <EOL> from param . parameterized import bothmethod <EOL> from ... core import HoloMap <EOL> from ... core . options import Store <EOL> from . . renderer import Renderer , MIME_TYPES <EOL> from . widgets import MPLSelectionWidget , MPLScrubberWidget <EOL> class OutputWarning ( param . Parameterized ) : pass <EOL> outputwarning = OutputWarning ( name = '<STR_LIT>' ) <EOL> class MPLRenderer ( Renderer ) : <EOL> """<STR_LIT>""" <EOL> drawn = { } <EOL> backend = param . String ( '<STR_LIT>' , doc = "<STR_LIT>" ) <EOL> fig = param . ObjectSelector ( default = '<STR_LIT>' , <EOL> objects = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:html>' , None , '<STR_LIT>' ] , doc = """<STR_LIT>""" ) <EOL> holomap = param . ObjectSelector ( default = '<STR_LIT>' , <EOL> objects = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None , '<STR_LIT>' ] , doc = """<STR_LIT>""" ) <EOL> mode = param . ObjectSelector ( default = '<STR_LIT:default>' , <EOL> objects = [ '<STR_LIT:default>' , '<STR_LIT>' , '<STR_LIT>' ] , doc = """<STR_LIT>""" ) <EOL> ANIMATION_OPTS = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , { } , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:10> } , [ ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT:html>' , None , { '<STR_LIT>' : <NUM_LIT:5> } , None ) <EOL> } <EOL> mode_formats = { '<STR_LIT>' : { '<STR_LIT:default>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:html>' , None , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:html>' , '<STR_LIT>' , None , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:html>' , None , '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { m : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:html>' , None , '<STR_LIT>' ] <EOL> for m in [ '<STR_LIT:default>' , '<STR_LIT>' , '<STR_LIT>' ] } } <EOL> counter = <NUM_LIT:0> <EOL> widgets = { '<STR_LIT>' : MPLScrubberWidget , <EOL> '<STR_LIT>' : MPLSelectionWidget } <EOL> def __call__ ( self , obj , fmt = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> plot , fmt = self . _validate ( obj , fmt ) <EOL> if plot is None : return <EOL> if isinstance ( plot , tuple ( self . widgets . values ( ) ) ) : <EOL> data = plot ( ) <EOL> elif fmt in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:html>' , '<STR_LIT>' ] : <EOL> data = self . _figure_data ( plot , fmt , ** ( { '<STR_LIT>' : self . dpi } if self . dpi else { } ) ) <EOL> else : <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> and mpl . __version__ [ : - <NUM_LIT:2> ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> anim = plot . anim ( fps = self . fps ) <EOL> data = self . _anim_data ( anim , fmt ) <EOL> data = self . _apply_post_render_hooks ( data , obj , fmt ) <EOL> return data , { '<STR_LIT>' : fmt , <EOL> '<STR_LIT>' : MIME_TYPES [ fmt ] } <EOL> @ classmethod <EOL> def plot_options ( cls , obj , percent_size ) : <EOL> """<STR_LIT>""" <EOL> from . plot import MPLPlot <EOL> factor = percent_size / <NUM_LIT> <EOL> obj = obj . last if isinstance ( obj , HoloMap ) else obj <EOL> options = Store . lookup_options ( cls . backend , obj , '<STR_LIT>' ) . options <EOL> fig_inches = options . get ( '<STR_LIT>' , MPLPlot . fig_inches ) <EOL> if isinstance ( fig_inches , ( list , tuple ) ) : <EOL> fig_inches = ( None if fig_inches [ <NUM_LIT:0> ] is None else fig_inches [ <NUM_LIT:0> ] * factor , <EOL> None if fig_inches [ <NUM_LIT:1> ] is None else fig_inches [ <NUM_LIT:1> ] * factor ) <EOL> else : <EOL> fig_inches = MPLPlot . fig_inches * factor <EOL> return dict ( { '<STR_LIT>' : fig_inches } , <EOL> ** Store . lookup_options ( cls . backend , obj , '<STR_LIT>' ) . options ) <EOL> @ bothmethod <EOL> def get_size ( self_or_cls , plot ) : <EOL> w , h = plot . state . get_size_inches ( ) <EOL> dpi = plot . state . dpi <EOL> return ( w * dpi , h * dpi ) <EOL> def _figure_data ( self , plot , fmt = '<STR_LIT>' , bbox_inches = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> fig = plot . state <EOL> if self . mode == '<STR_LIT>' : <EOL> manager = self . get_figure_manager ( plot . state ) <EOL> if manager is None : return '<STR_LIT>' <EOL> self . counter += <NUM_LIT:1> <EOL> manager . show ( ) <EOL> return '<STR_LIT>' <EOL> elif self . mode == '<STR_LIT>' : <EOL> import mpld3 <EOL> fig . dpi = self . dpi <EOL> mpld3 . plugins . connect ( fig , mpld3 . plugins . MousePosition ( fontsize = <NUM_LIT> ) ) <EOL> if fmt == '<STR_LIT>' : <EOL> return mpld3 . fig_to_dict ( fig ) <EOL> else : <EOL> return "<STR_LIT>" + mpld3 . fig_to_html ( fig ) + "<STR_LIT>" <EOL> traverse_fn = lambda x : x . handles . get ( '<STR_LIT>' , None ) <EOL> extra_artists = list ( chain ( * [ artists for artists in plot . traverse ( traverse_fn ) <EOL> if artists is not None ] ) ) <EOL> kw = dict ( <EOL> format = fmt , <EOL> facecolor = fig . get_facecolor ( ) , <EOL> edgecolor = fig . get_edgecolor ( ) , <EOL> dpi = self . dpi , <EOL> bbox_inches = bbox_inches , <EOL> bbox_extra_artists = extra_artists <EOL> ) <EOL> kw . update ( kwargs ) <EOL> try : <EOL> kw = self . _compute_bbox ( fig , kw ) <EOL> except : <EOL> pass <EOL> bytes_io = BytesIO ( ) <EOL> fig . canvas . print_figure ( bytes_io , ** kw ) <EOL> data = bytes_io . getvalue ( ) <EOL> if fmt == '<STR_LIT>' : <EOL> data = data . decode ( '<STR_LIT:utf-8>' ) <EOL> return data <EOL> def get_figure_manager ( self , fig ) : <EOL> from matplotlib . backends . backend_nbagg import new_figure_manager_given_figure <EOL> manager = new_figure_manager_given_figure ( self . counter , fig ) <EOL> for ax in fig . get_axes ( ) : <EOL> if isinstance ( ax , Axes3D ) : <EOL> ax . mouse_init ( ) <EOL> return manager <EOL> def _anim_data ( self , anim , fmt ) : <EOL> """<STR_LIT>""" <EOL> ( writer , _ , anim_kwargs , extra_args ) = self . ANIMATION_OPTS [ fmt ] <EOL> if extra_args != [ ] : <EOL> anim_kwargs = dict ( anim_kwargs , extra_args = extra_args ) <EOL> if self . fps is not None : anim_kwargs [ '<STR_LIT>' ] = max ( [ int ( self . fps ) , <NUM_LIT:1> ] ) <EOL> if self . dpi is not None : anim_kwargs [ '<STR_LIT>' ] = self . dpi <EOL> if not hasattr ( anim , '<STR_LIT>' ) : <EOL> with NamedTemporaryFile ( suffix = '<STR_LIT>' % fmt ) as f : <EOL> anim . save ( f . name , writer = writer , ** anim_kwargs ) <EOL> video = open ( f . name , "<STR_LIT:rb>" ) . read ( ) <EOL> return video <EOL> def _compute_bbox ( self , fig , kw ) : <EOL> """<STR_LIT>""" <EOL> fig_id = id ( fig ) <EOL> if kw [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if not fig_id in MPLRenderer . drawn : <EOL> fig . set_dpi ( self . dpi ) <EOL> renderer = fig . _cachedRenderer <EOL> bbox_inches = fig . get_tightbbox ( renderer ) <EOL> bbox_artists = kw . pop ( "<STR_LIT>" , [ ] ) <EOL> bbox_artists += fig . get_default_bbox_extra_artists ( ) <EOL> bbox_filtered = [ ] <EOL> for a in bbox_artists : <EOL> bbox = a . get_window_extent ( renderer ) <EOL> if isinstance ( bbox , tuple ) : <EOL> continue <EOL> if a . get_clip_on ( ) : <EOL> clip_box = a . get_clip_box ( ) <EOL> if clip_box is not None : <EOL> bbox = Bbox . intersection ( bbox , clip_box ) <EOL> clip_path = a . get_clip_path ( ) <EOL> if clip_path is not None and bbox is not None : <EOL> clip_path = clip_path . get_fully_transformed_path ( ) <EOL> bbox = Bbox . intersection ( bbox , <EOL> clip_path . get_extents ( ) ) <EOL> if bbox is not None and ( bbox . width != <NUM_LIT:0> or <EOL> bbox . height != <NUM_LIT:0> ) : <EOL> bbox_filtered . append ( bbox ) <EOL> if bbox_filtered : <EOL> _bbox = Bbox . union ( bbox_filtered ) <EOL> trans = Affine2D ( ) . scale ( <NUM_LIT:1.0> / self . dpi ) <EOL> bbox_extra = TransformedBbox ( _bbox , trans ) <EOL> bbox_inches = Bbox . union ( [ bbox_inches , bbox_extra ] ) <EOL> pad = plt . rcParams [ '<STR_LIT>' ] <EOL> bbox_inches = bbox_inches . padded ( pad ) <EOL> MPLRenderer . drawn [ fig_id ] = bbox_inches <EOL> kw [ '<STR_LIT>' ] = bbox_inches <EOL> else : <EOL> kw [ '<STR_LIT>' ] = MPLRenderer . drawn [ fig_id ] <EOL> return kw <EOL> @ classmethod <EOL> @ contextmanager <EOL> def state ( cls ) : <EOL> try : <EOL> cls . _rcParams = dict ( mpl . rcParams ) <EOL> yield <EOL> finally : <EOL> mpl . rcParams = cls . _rcParams <EOL> @ classmethod <EOL> def validate ( cls , options ) : <EOL> """<STR_LIT>""" <EOL> if options [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> outputwarning . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if options [ '<STR_LIT>' ] == '<STR_LIT>' and options [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> outputwarning . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return options </s>
