
    Le"                     F   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
Zd dlZd dl
mZ d dlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ  G d dej                  Z ed          Zddi g dfddddiddgfddddig fdddd idfd!Z G d" d#e          Z dS )$    N)contextmanager)BytesIO)chain)NamedTemporaryFile)pyplot)
bothmethod   )HoloMap)Store   )	HTML_TAGS
MIME_TYPESRenderer   )get_old_rcparamsget_tight_bboxc                       e Zd ZdS )OutputWarningN)__name__
__module____qualname__     ?lib/python3.11/site-packages/holoviews/plotting/mpl/renderer.pyr   r      s          r   r   Warning)nameffmpegwebm)z-vcodecz
libvpx-vp9z-b1000kmp4codeclibx264z-pix_fmtyuv420ppillowgiffps
   html   )r   r    r%   scrubberc                      e Zd ZdZi Z ej        dd          Z ej        dd          Z	 ej
        dg d	d
          Z ej
        dg dd          Z ej        dd          Z ej
        ddg          Zg d	g ddZdZd Zed             Zed             Zd dZd Zd Zeed                         Zed!d            ZdS )"MPLRenderera  
    Exporter used to render data from matplotlib, either to a stream
    or directly to file.

    The __call__ method renders an HoloViews component to raw data of
    a specified matplotlib format.  The save method is the
    corresponding method for saving a HoloViews objects to disk.

    The save_fig and save_anim methods are used to save matplotlib
    figure and animation objects. These match the two primary return
    types of plotting class implemented with matplotlib.
    
matplotlibzThe backend name.)docH   z5
        The render resolution in dpi (dots per inch))defaultr.   auto)pngsvgpdfpgfr(   Nr1   zc
        Output render format for static figures. If None, no figure
        rendering will occur. )r0   objectsr.   )widgetsr*   r   r    r%   Nr1   zu
        Output render multi-frame (typically animated) format. If
        None, no multi-frame rendering will occur.Fzs
        Whether to enable interactive plotting allowing interactive
        plotting with explicitly calling show.r0   )r0   r6   )r7   r*   r   r    r%   r(   Nr1   )figholomapr   c                      j         r8t          |t                    r fd|D             S                      |          S ddlm} d|_        	 g }t          |t                    r|n|g}|D ]*}|                                         |                     +t          j	                     n# t          $ r  w xY w	 d|_        n# d|_        w xY wt          |          dk    r|d         n|S )zc
        Renders the supplied object and displays it using the active
        GUI backend.
        c                 :    g | ]}                     |          S r   )get_plot).0oselfs     r   
<listcomp>z$MPLRenderer.show.<locals>.<listcomp>T   s%    666Qa((666r   r   MPLPlotFTr   )interactive
isinstancelistr<   plotrB   _close_figuresappendpltshow	Exceptionlen)r?   objrB   plotsr6   r>   s   `     r   rJ   zMPLRenderer.showM   s)   
  	&#t$$ 76666#6666==%%%!!!!!!!&		*E'T22=ccG / /T]]1--....HJJJJ 	 	 		  &*G""TG"))))u::??uQxx5s   AB, +C ,B88C 	Cc                     ddl m} |dz  }t          |t                    r|j        n|}t          j        | j        |d          j        }|	                    d|j
                  |z  }t          d|ifi |                    |d          j        S )aA  
        Given a holoviews object and a percentage size, apply heuristics
        to compute a suitable figure size. For instance, scaling layouts
        and grids linearly can result in unwieldy figure sizes when there
        are a large number of elements. As ad hoc heuristics are used,
        this functionality is kept separate from the plotting classes
        themselves.

        Used by the IPython Notebook display hooks and the save
        utility. Note that this can be overridden explicitly per object
        using the fig_size and size plot options.
        r   rA   g      Y@rF   fig_size)rF   rB   rD   r
   lastr   lookup_optionsbackendoptionsgetrP   dict)clsrM   percent_sizerB   factorrT   rP   s          r   plot_optionszMPLRenderer.plot_optionsf   s     	"!!!!!%$S'22;chh&s{C@@H;;z7+;<<VCZ) C C,,S&99AC C 	Cr   c                     |j                                         \  }}| j        r| j        n|j         j        }t          ||z            t          ||z            fS N)stateget_size_inchesdpiint)self_or_clsrF   whr_   s        r   get_sizezMPLRenderer.get_size~   sN    z))++1!,DkoodjnAcE

C#JJ''r   tightc                    |dv rdt          j        |j                  5  |                    | j                  }ddd           n# 1 swxY w Y   |                     ||          }n|j        }d }	t          t          j	        d |
                    |	          D                                 }
t          ||                                |                                | j        ||
          }|                    |           	 |                     ||          }n# t"          $ r Y nw xY wt%                      } |j        j        |fi | |                                }|r}t-          j        |                              d          }t2          |         t4          |         }}t4          d	                             ||
          }|                    ||d          }|S |dk    r|                    d          }|S )a  
        Render matplotlib figure object and return the corresponding
        data.  If as_script is True, the content will be split in an
        HTML and a JS component.

        Similar to IPython.core.pylabtools.print_figure but without
        any IPython dependency.
        )r%   r    r   )rc)r&   Nc                 8    | j                             dd           S )Nbbox_extra_artists)handlesrU   )xs    r   <lambda>z*MPLRenderer._figure_data.<locals>.<lambda>   s    AIMM2F$M$M r   c              3      K   | ]}||V  	d S r\   r   )r=   artistss     r   	<genexpr>z+MPLRenderer._figure_data.<locals>.<genexpr>   s3       $< $<'.': %,':':':':$< $<r   )format	facecolor	edgecolorr_   bbox_inchesri   zutf-8base64)	mime_typeb64 )srcru   cssr3   )mpl
rc_contextfig_rcparamsanimr&   
_anim_datar]   rE   r   from_iterabletraverserV   get_facecolorget_edgecolorr_   update_compute_bboxrK   r   canvasprint_figuregetvaluert   	b64encodedecoder   r   rp   )r?   rF   fmtrs   	as_scriptkwargsr}   datar8   traverse_fnextra_artistskwbytes_iorv   ru   tagrx   r(   s                     r   _figure_datazMPLRenderer._figure_data   sR    (((4#4555 / /yyTXy../ / / / / / / / / / / / / / /??4--DD*CMMK # $< $<4==;U;U $< $< $< < < M
 ++--++--H'#0  B IIf''R00   yyH#CJ#H33333$$&&D 	"4((//88C)#	#YH%,,yc,JJC::#:CCDK%<<;;w''Ds#   AAAD 
D)(D)c                    t           |         \  }}}}|g k    rt          ||          }| j        &t          t	          | j                  dg          |d<   | j        
| j        |d<   t          |d          st          d| d	          5 } |j        |j	        fd
|i| |
                                }ddd           n# 1 swxY w Y   |                                 t          j        |j	                   |S )zY
        Render a matplotlib animation object and return the corresponding data.
        )
extra_argsNr   r&   r_   _encoded_video.F)suffixdeletewriter)ANIMATION_OPTSrV   r&   maxr`   r_   hasattrr   saver   readcloseosremove)	r?   r}   r   r   _anim_kwargsr   fvideos	            r   r~   zMPLRenderer._anim_data   s?    0>c/B,K{zBBBK8c3tx==!:L6M6MU!38dhU!3t-.. 	#9s99UCCC !q	!&???;???! ! ! ! ! ! ! ! ! ! ! ! ! ! ! GGIIIIafs   *CC	Cc                    t          |          }|d         dk    r|t          j        vr|                    | j                   |j                                         |                    dg           }t          j	        d         }t          |||          }|t          j        |<   ||d<   nt          j        |         |d<   |S )ai  
        Compute the tight bounding box for each figure once, reducing
        number of required canvas draw calls from N*2 to N+1 as a
        function of the number of frames.

        Tight bounding box computing code here mirrors:
        matplotlib.backend_bases.FigureCanvasBase.print_figure
        as it hasn't been factored out as a function.
        rs   re   ri   zsavefig.pad_inches)pad)idr,   drawnset_dpir_   r   drawpoprz   rcParamsr   )r?   r8   r   fig_idr   r   rs   s          r   r   zMPLRenderer._compute_bbox   s     Cm''[...DH%%%
!!! "';R @ @l#78,S-SIII,7!&)$/=!!$/$5f$==!	r   c              #   P  K   t                      }	 || _        d V  t          j                                         t          j                            | j                   d S # t          j                                         t          j                            | j                   w xY wr\   )r   	_rcParamsrz   r   clearr   )rW   old_rcparamss     r   r]   zMPLRenderer.state   s       ())	/(CMEEEL   L..... L   L....s   A! !AB%Tc                 \    ddl m}  |j                    }|dvr |j        d           dS dS )z/
        Initialize matplotlib backend
        r   N)aggz'module://ipykernel.pylab.backend_inliner   )matplotlib.pyplotr   get_backendswitch_backend)rW   inlinerI   rS   s       r   load_nbzMPLRenderer.load_nb   sS    
 	('''''!#/##LLLCu%%%%% MLr   N)re   F)T)r   r   r   __doc__r   paramStringrS   Integerr_   ObjectSelectorr8   r9   BooleanrC   modemode_formatscounterrJ   classmethodrZ   r   rd   r   r~   r   r   r]   r   r   r   r   r,   r,   !   s         Eel<-@AAAGb '8 	9 	9 	9C %
v(> (> (>D"# # #C #e"6+e+e+e l67 7 7G
  %- 42 3 3 3K  5	I;GGGD  RQQ 6  6  67 7L G6 6 62 C C [C. ( ( Z(1 1 1 1h  (  0 / / ^ [/ & & & [& & &r   r,   )!rt   r   
contextlibr   ior   	itertoolsr   tempfiler   r-   rz   r   r   rI   param.parameterizedr   corer
   core.optionsr   rendererr   r   r   utilr   r   Parameterizedr   outputwarningr   r,   r   r   r   <module>r      s    				 % % % % % %             ' ' ' ' ' '      $ $ $ $ $ $ * * * * * *       ! ! ! ! ! ! 6 6 6 6 6 6 6 6 6 6 2 2 2 2 2 2 2 2 . - - - -E' - - -9--- vr5557egy1#%eeR["-qz40 X& X& X& X& X&( X& X& X& X& X&r   