
    -Ph.                     b    d Z ddlZddlZddlmZ ddlZddlmZ ddlmZ  G d de	          Z
dS )	z
Matplotlib Exporter
===================
This submodule contains tools for crawling a matplotlib figure and exporting
relevant pieces to a renderer.
    N   )utils)
transforms)FigureCanvasAggc                   t    e Zd ZdZddZd Ze	 dd            Zd Zd	 Z	d
 Z
ddZddZddZ	 ddZd ZdS )Exportera  Matplotlib Exporter

    Parameters
    ----------
    renderer : Renderer object
        The renderer object called by the exporter to create a figure
        visualization.  See mplexporter.Renderer for information on the
        methods which should be defined within the renderer.
    close_mpl : bool
        If True (default), close the matplotlib figure as it is rendered. This
        is useful for when the exporter is used within the notebook, or with
        an interactive matplotlib backend.
    Tc                 "    || _         || _        d S N)	close_mplrenderer)selfr   r   s      h/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/plotly/matplotlylib/mplexporter/exporter.py__init__zExporter.__init__    s    "     c                     |j         t          |          }|                    t          j                    d|j                   | j        rddlm} |	                    |           | 
                    |           dS )z
        Run the exporter on the given figure

        Parmeters
        ---------
        fig : matplotlib.Figure instance
            The figure to export
        Npng)formatdpir   )canvasr   savefigioBytesIOr   r   matplotlib.pyplotpyplotclose	crawl_fig)r   figr   plts       r   runzExporter.run$   s|     :$S))FBJLLCG<<<> 	++++++IIcNNNsr   NFc                    t          | t          j                  rt          j        d           ||| |z
                      |          }|} d}|Wd|j        fd|j        fd|j        j	        fdt          j
                    ffD ]#\  }}|                     |          r	|| |z
  } } n$|1|r||                     |          | fS ||                     |          fS |r|| fS |S )a?  Process the transform and convert data to figure or data coordinates

        Parameters
        ----------
        transform : matplotlib Transform object
            The transform applied to the data
        ax : matplotlib Axes object (optional)
            The axes the data is associated with
        data : ndarray (optional)
            The array of data to be transformed.
        return_trans : bool (optional)
            If true, return the final transform of the data
        force_trans : matplotlib.transform instance (optional)
            If supplied, first force the data to this transform

        Returns
        -------
        code : string
            Code is either "data", "axes", "figure", or "display", indicating
            the type of coordinates output.
        transform : matplotlib transform
            the transform used to map input data to output data.
            Returned only if return_trans is True
        new_data : ndarray
            Data transformed to match the given coordinate code.
            Returned only if data is specified
        zMBlended transforms not yet supported. Zoom behavior may not work as expected.Ndisplaydataaxesfigure)
isinstancer   BlendedGenericTransformwarningswarn	transform	transData	transAxesr$   transFigureIdentityTransformcontains_branch)r)   axr"   return_transforce_transcodectranss           r   process_transformzExporter.process_transform8   s9   > i!CDD 	M:  
 "!K/::4@@#I>&&2901J8::;	  5 ,,U33 '()e*;)DE  7Y0066	AAY006666 Y&r   c                     | j                             |t          j        |                    5  |j        D ]}|                     |           	 ddd           dS # 1 swxY w Y   dS )z%Crawl the figure and process all axes)r   propsN)r   draw_figurer   get_figure_propertiesr#   crawl_ax)r   r   r/   s      r   r   zExporter.crawl_figy   s    ]&&3e6QRU6V6V&WW 	" 	"h " "b!!!!"	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s    AA!$A!c                 8   | j                             |t          j        |                    5  |j        D ]}|                     ||           |j        D ]}|                     ||           t          |j	        j
        |j        j
        |j        gg d          D ]G\  }}t          |d          r2|                                r|                     |||j        |           H|j        D ]7}t#          |t$          j        j                  r|                     ||           8|j        D ]}|                     ||           |j        D ]}|                     ||           |j        D ]}|                     ||           |                                }	|	gt          j        ||	          }
| j                             |	|
          5  |
d         r|                     ||	           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z.Crawl the axes and process all elements within)r/   r7   )xlabelylabeltitleget_text)r1   	text_typeN)legendr7   visible)r   	draw_axesr   get_axes_propertieslines	draw_linetexts	draw_textzipxaxislabelyaxisr>   hasattrr?   r+   artistsr%   
matplotlibtextTextpatches
draw_patchcollectionsdraw_collectionimages
draw_image
get_legendget_legend_propertiesdraw_legendcrawl_legend)r   r/   linerP   ttpartistpatch
collectionimagerA   r7   s              r   r:   zExporter.crawl_ax   s   ]$$%2KB2O2O$PP 	6 	6 ) )r4(((( ) )r4(((( :---  V V	c 4,, V VNN2tQTNUUU* / /fjo&:;; /NN2v... + +E**** n 5 5
$$R4444 + +E****]]__F!3B??]..fE.JJ 6 6Y' 6))"f5556 6 6 6 6 6 6 6 6 6 6 6 6 6 63	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s6   FHG7+H7G;	;H>G;	?HHHc                    t          t          j        |j        d                    }|                    |j                   |D ]}|                    d|                                z              t          |t          j
        j                  r*|                    |                                dz
             	 t          |t          j
        j                  r|                     |||j                   nt          |t          j        j                  r6|                                dk    r|                     |||j                   nt          |t          j        j                  r|                     |||j                   nTt          |t          j        j                  r|                     |||j                   nt3          j        d|z             # t6          $ r t3          j        d|z             Y w xY wd	S )
zE
        Recursively look through objects in legend children
        T)skipContainersg    .Ar   r1   None)force_pathtransz Legend element %s not impementedN)listr   iter_all_children_legend_boxappendlegendPatch
set_zorder
get_zorderr%   rO   rR   FancyBboxPatchPatchrS   r+   rP   rQ   r?   rH   rE   Line2DrF   rT   
CollectionrU   r'   r(   NotImplementedError)r   r/   rA   legendElementschilds        r   r[   zExporter.crawl_legend   s    #F$6tLLL
 
 	f0111# 	J 	JES5#3#3#5#55666 %!3!BCC 9  !1!1!3!3a!7888JeZ%7%=>> 
NOOB2<OHHHHz';<< N~~''611r5blKKKz'7'>?? NNN2u",NGGGGz'='HII N((UBL(QQQQM"Du"LMMM& J J J@5HIIIIIJ+	J 	Js   <D#G!!!HHc                    |                      |                                ||                                |          \  }}t          j        |          }|d         |d         dk    rd}t          j        |          }|d         dv s|d         d	         j        d	k    rd}|                                }|s|r"| j        	                    ||||||
           dS dS )z5Process a matplotlib line and call renderer.draw_linerd   	dasharrayN	drawstyledefaultmarker)re   noneN
markerpathr   )r"   coordinates	linestylemarkerstylerK   mplobj)
r5   get_transform
get_xydatar   get_line_styleget_marker_stylesize	get_labelr   draw_marked_line)	r   r/   r\   r1   r|   r"   r}   r~   rK   s	            r   rF   zExporter.draw_line   s    22  "doo&7&7[ 3 
 
T (..	[!)i.D	.Q.QI,T22!%;;;<(+0A55K   	) 	M**'#' +     	 	r   c                 &   |                                 }|rz|                                }|                                }|                     ||||          \  }}t	          j        |          }	| j                            |||||	|           dS dS )z<Process a matplotlib text object and call renderer.draw_textrd   )rP   positionr|   r@   styler   N)r?   r   get_positionr5   r   get_text_styler   rH   )
r   r/   rP   r1   r@   contentr)   r   coordsr   s
             r   rH   zExporter.draw_text   s    --// 	**,,I((**H#552x[  6    FH (..EM##!"# $     	 	r   c                 F   t          j        |                                          \  }}|                                }|                     ||||          \  }}t          j        ||                                          }| j                            |||||           dS )z=Process a matplotlib patch object and call renderer.draw_pathrd   )fill)r"   r|   	pathcodesr   r   N)	r   SVG_pathget_pathr   r5   get_path_styleget_fillr   	draw_path)	r   r/   r_   r1   verticesr   r)   r|   r}   s	            r   rS   zExporter.draw_patch   s    #nU^^-=-=>>)''))	 $ 6 6r8 !7 !
 !
X (U^^5E5EFFF	# 	  	
 	
 	
 	
 	
r   c           
          |                                 \  }}}                     |||          \  }}                               }	d |D             }
 fd|
D             }
|                                }	 d |D             }n# t          $ r Y nw xY w|                                |                                |                                |j        |                                d}ddd}t          |d	          r||
                                         nd} j                            |
|	||||||
           dS )zAProcess a matplotlib collection and call renderer.draw_collectionrd   c                 6    g | ]}t          j        |          S  )r   r   ).0paths     r   
<listcomp>z,Exporter.draw_collection.<locals>.<listcomp>  s"    BBBD5>$//BBBr   c                 j    g | ]/}                     |d                    d         |d         f0S )r   rd   r   )r5   )r   r   r/   rf   r   r)   s     r   r   z,Exporter.draw_collection.<locals>.<listcomp>  sc     
 
 
  &&r47 '   Q	
 
 
r   c                 6    g | ]}|                                 S r   )
get_matrix)r   ts     r   r   z,Exporter.draw_collection.<locals>.<listcomp>  s     GGG!q||~~GGGr   )	linewidth	facecolor	edgecoloralphazorderbeforeafter)r"   screenget_offset_position)pathspath_coordinatespath_transformsoffsetsoffset_coordinatesoffset_orderstylesr   N)_prepare_pointsr5   get_transformsAttributeErrorget_linewidthsget_facecolorsget_edgecolors_alpharm   rM   r   r   draw_path_collection)r   r/   r`   rf   force_offsettranstransOffsetr   r   offset_coordspath_coordsprocessed_pathsr   r   offset_dictr   r)   s   `` `           @r   rU   zExporter.draw_collection   s    4>3M3M3O3O0K%!%!7!7W2C "8 "
 "
w ,,Y,XXBBEBBB
 
 
 
 
 
 
 (
 
 
 %3355	 HGGGGOO 	 	 	D	
 $2244#2244#2244& ++--
 
  (7;; z#899K
668899 	 	**!(+,% 	+ 		
 		
 		
 		
 		
s   B 
BBc                     | j                             t          j        |          |                                d|                                |                                d|           dS )z>Process a matplotlib image object and call renderer.draw_imager"   )r   r   )imdataextentr|   r   r   N)r   rW   r   image_to_base64
get_extent	get_alpharm   )r   r/   ra   s      r   rW   zExporter.draw_image5  so      (//##%%!OO--9I9I9K9KLL 	! 	
 	
 	
 	
 	
r   )T)NNFNr
   )NN)__name__
__module____qualname____doc__r   r   staticmethodr5   r   r:   r[   rF   rH   rS   rU   rW   r   r   r   r   r      s         ! ! ! !  ( GK> > > \>@" " "6 6 6>J J J@   2   &
 
 
 
" GK9
 9
 9
 9
v
 
 
 
 
r   r   )r   r'   r    r   rO   r   matplotlib.backends.backend_aggr   objectr   r   r   r   <module>r      s      				           ! ! ! ! ! ! ; ; ; ; ; ;l
 l
 l
 l
 l
v l
 l
 l
 l
 l
r   