U
    TZßfn.  ã                
   @   s(  d Z ddlZddlZddlZddl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 dd	„ Zd
d„ ZeddddddœƒZdd„ ZejdejdejdejdejdiZd2dd„Zd3dd„Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Z d&d'„ Z!d(d)„ Z"d*d+„ Z#d4d,d-„Z$d.d/„ Z%d0d1„ Z&dS )5zk
Utility Routines for Working with Matplotlib Objects
====================================================
é    N)ÚcolorConverter)ÚPath)ÚMarkerStyle)ÚAffine2D)Útickerc                 C   s   | dkst  | ¡d dkrdS t  | ¡d dkrNt  | ¡}djdd„ |D ƒŽ S t  | ¡}d	d
 dd„ |dd… D ƒ¡ d
 t|d ƒ d S dS )z8Convert matplotlib color code to hex color or RGBA colorNé   r   Únoneé   z#{0:02X}{1:02X}{2:02X}c                 s   s   | ]}t d | ƒV  qdS ©éÿ   N)Úint)Ú.0Úc© r   úI/tmp/pip-unpacked-wheel-5ksk5baj/plotly/matplotlylib/mplexporter/utils.pyÚ	<genexpr>   s     zexport_color.<locals>.<genexpr>zrgba(z, c                 s   s$   | ]}t tt |d  ¡ƒƒV  qdS r
   )Ústrr   ÚnpÚround)r   Úvalr   r   r   r       s     ú))r   Zto_rgbaZto_rgbÚformatÚjoinr   )ÚcolorZrgbr   r   r   r   Úexport_color   s     

ÿþ
ýüÿr   c                 C   s   t dd„ |  ¡ D ƒƒS )z5Convert a many-to-one mapping to a one-to-one mappingc                 s   s$   | ]\}}|D ]}||fV  qqd S ©Nr   )r   Úkeysr   Úkeyr   r   r   r   )   s       z_many_to_one.<locals>.<genexpr>)ÚdictÚitems)Z
input_dictr   r   r   Ú_many_to_one'   s    r    r   z6,6z2,2z4,4,2,4))Úsolidú-)NN)Zdashedz--)Zdottedú:)Zdashdotz-.)Ú ú ÚNoner   c                 C   s`   | j  dd¡dk	r$d tt| jƒ¡S |  ¡ }t |d¡}|dkrXt 	d 
|¡¡ td }|S dS )ap  Get an SVG dash array for the given matplotlib linestyle

    Parameters
    ----------
    obj : matplotlib object
        The matplotlib line or path object, which must have a get_linestyle()
        method which returns a valid matplotlib line code

    Returns
    -------
    dasharray : string
        The HTML/SVG dasharray code associated with the object.
    Ú_dashSeqNú,z	not foundz:line style '{0}' not understood: defaulting to solid line.r!   )Ú__dict__Úgetr   Úmapr   r'   Zget_linestyleÚ
LINESTYLESÚwarningsÚwarnr   )ÚobjZlsÚ	dasharrayr   r   r   Úget_dasharray7   s    ÿÿr1   ÚLÚMÚSÚCÚZFc                 C   sr   |dk	r|   |¡} dd„ | j|dD ƒ}|s:t d¡g fS t|Ž \}}t ttj|Ž ƒ¡ 	dd¡}|t|ƒfS dS )aÓ  Construct the vertices and SVG codes for the path

    Parameters
    ----------
    path : matplotlib.Path object

    transform : matplotlib transform (optional)
        if specified, the path will be transformed before computing the output.

    Returns
    -------
    vertices : array
        The shape (M, 2) array of vertices of the Path. Note that some Path
        codes require multiple vertices, so the length of these vertices may
        be longer than the list of path codes.
    path_codes : list
        A length N list of single-character path codes, N <= M. Each code is
        a single character, in ['L','M','S','C','Z']. See the standard SVG
        path specification for a description of these.
    Nc                 S   s*   g | ]"\}}|t jkr|ng t| f‘qS r   )r   Ú	CLOSEPOLYÚ	PATH_DICT)r   ÚverticesZ	path_coder   r   r   Ú
<listcomp>t   s   ÿzSVG_path.<locals>.<listcomp>)Úsimplify)r   é   éÿÿÿÿr<   )
ZtransformedZiter_segmentsr   ÚzerosÚzipÚarrayÚlistÚ	itertoolsÚchainZreshape)ÚpathZ	transformr;   Z	vc_tuplesr9   Úcodesr   r   r   ÚSVG_path\   s    

þrF   Tc                 C   sz   i }|   ¡ |d< |d dkr$d|d< t|  ¡ ƒ|d< |rJt|  ¡ ƒ|d< nd|d< |  ¡ |d< t| ƒ|d< |  ¡ |d	< |S )
z4Get the style dictionary for matplotlib path objectsÚalphaNr	   Ú	edgecolorÚ	facecolorr   Ú	edgewidthr0   Úzorder)Ú	get_alphar   Zget_edgecolorÚget_facecolorÚget_linewidthr1   Ú
get_zorder)rD   ÚfillÚstyler   r   r   Úget_path_style‚   s    rR   c                 C   sh   i }|   ¡ |d< |d dkr$d|d< t|  ¡ ƒ|d< |  ¡ |d< t| ƒ|d< |  ¡ |d< |  ¡ |d< |S )	z4Get the style dictionary for matplotlib line objectsrG   Nr	   r   Z	linewidthr0   rK   Z	drawstyle)rL   r   Ú	get_colorrN   r1   rO   Zget_drawstyle)ÚlinerQ   r   r   r   Úget_line_style“   s    rU   c                 C   s²   i }|   ¡ |d< |d dkr$d|d< t|  ¡ ƒ|d< t|  ¡ ƒ|d< |  ¡ |d< |  ¡ |d< t|  ¡ ƒ}|  ¡ }| ¡ t	ƒ  
|| ¡ }t| ¡ |ƒ|d< ||d	< |  ¡ |d
< |S )z6Get the style dictionary for matplotlib marker objectsrG   Nr	   rI   rH   rJ   ÚmarkerZ
markerpathÚ
markersizerK   )rL   r   Zget_markerfacecolorZget_markeredgecolorZget_markeredgewidthZ
get_markerr   Zget_markersizeZget_transformr   ÚscalerF   Úget_pathrO   )rT   rQ   ZmarkerstylerW   Zmarkertransformr   r   r   Úget_marker_style¡   s$     ÿrZ   c                 C   s~   i }|   ¡ |d< |d dkr$d|d< |  ¡ |d< t|  ¡ ƒ|d< |  ¡ |d< |  ¡ |d< | j|d< |  ¡ |d	< |  ¡ |d
< |S )z.Return the text style dict for a text instancerG   Nr	   Úfontsizer   ZhalignZvalignZmalignZrotationrK   )	rL   Úget_sizer   rS   Zget_horizontalalignmentZget_verticalalignmentZ_multialignmentZget_rotationrO   )ÚtextrQ   r   r   r   Úget_text_style¸   s    
r^   c                 C   s‚  i }| j  dd¡}t| tjjƒr8|r.d|d< qld|d< n4t| tjjƒr^|rTd|d< qld|d< ntd | ¡ƒ‚|  	¡ }t
|ƒ ƒ|d	< t|tjƒržt|ƒ ƒ|d
< nd|d
< |  ¡ }t|tjƒrÄd|d< nnt|tjƒràt|jƒ|d< nRt|tjƒrt|jjd  ¡ ƒ|d< n*tdd„ |  ¡ D ƒƒs*d|d< nd|d< |  ¡ |d< |  ¡ }|r^|d  ¡ |d< nd|d< t| ƒ|d< |  ¡ |d< |S )z=Return the property dictionary for a matplotlib.Axis instanceÚlabel1OnTZbottomÚpositionÚtopÚleftÚrightz{0} should be an Axis instanceZnticksZ
tickvaluesNr$   Z
tickformatr   c                 s   s   | ]}|  ¡ V  qd S r   )Úget_visible)r   Úlabelr   r   r   r   ê   s     z&get_axis_properties.<locals>.<genexpr>rX   r[   ZgridÚvisible)Ú_major_tick_kwr*   Ú
isinstanceÚ
matplotlibÚaxisZXAxisZYAxisÚ
ValueErrorr   Zget_major_locatorÚlenr   ZFixedLocatorrA   Zget_major_formatterZNullFormatterZFixedFormatterÚseqZFuncFormatterÚfuncÚargsÚvaluesÚanyZget_ticklabelsÚ	get_scaleZget_fontsizeÚget_grid_stylerd   )rj   Úpropsr_   ÚlocatorÚ	formatterÚlabelsr   r   r   Úget_axis_propertiesÈ   sD    





rx   c                 C   sb   |   ¡ }| jd rVt|ƒdkrVt|d  ¡ ƒ}|d  ¡ }t|d ƒ}td|||dS ddiS d S )NÚgridOnr   T)ry   r   r0   rG   F)Zget_gridlinesrg   rl   r   rS   rL   r1   r   )rj   Z	gridlinesr   rG   r0   r   r   r   rs     s    rs   c                 C   s   |   ¡ |  ¡ | jdœS )N)ZfigwidthZ	figheightÚdpi)Zget_figwidthZget_figheightrz   )Zfigr   r   r   Úget_figure_properties  s    ýr{   c              
      s`  t | j ¡ ƒ| j ¡ |  ¡ j|  ¡ | j|  ¡ | j 	¡ t
| jƒt
| jƒgdœ}dD ]}t| |d ƒ‰ t| d |¡ƒƒ }|}tˆ jtjjƒrd}zdd l‰ddlm} W n tk
rÀ   d ‰Y nX ˆd k	rútˆ j|ƒrú‡ ‡fdd	„|D ƒ}d
d	„ |D ƒ}ndd	„ tj |¡D ƒ}nˆ  ¡ }|dkr6td ˆ  ¡ ¡ƒ‚|||d < |||d < |||d < qP|S )N)ZaxesbgZaxesbgalphaÚboundsZdynamicÚaxisonZframe_onZpatch_visibleÚaxes)ÚxÚyrj   z
get_{0}limÚdater   )ÚPeriodConverterc                    s    g | ]}ˆj t|ƒˆ jd ‘qS ))ZordinalÚfreq)ZPeriodr   rƒ   ©r   Úd©rj   Úpdr   r   r:   .  s     z'get_axes_properties.<locals>.<listcomp>c              	   S   s.   g | ]&}|j |jd  |j|j|j|jdf‘qS )r	   r   )ÚyearÚmonthÚdayÚhourÚminuteÚsecondr„   r   r   r   r:   /  s   ÿc              
   S   s4   g | ],}|j |jd  |j|j|j|j|jd f‘qS )r	   gü©ñÒMbP?)rˆ   r‰   rŠ   r‹   rŒ   r   Úmicrosecondr„   r   r   r   r:   4  s   
øù)r   ZlinearÚlogzUnknown axis scale: {0}rX   ÚlimÚdomain)r   ÚpatchrM   rL   Úget_positionr|   Zget_navigater}   Zget_frame_onrd   rx   ZxaxisZyaxisÚgetattrr   rh   Ú	converterri   ÚdatesZDateConverterZpandasZpandas.tseries.converterr‚   ÚImportErrorZnum2daterr   rk   )Úaxrt   Zaxnamer‘   r   rX   r‚   Z_datesr   r†   r   Úget_axes_properties  sF    ø

þ

ö
r™   c                 c   sT   t | dƒrJt|  ¡ ƒdkrJ|  ¡ D ]$}|s0|V  t||ƒD ]
}|V  q:q"n| V  dS )z­
    Returns an iterator over all childen and nested children using
    obj's get_children() method

    if skipContainers is true, only childless objects are returned.
    Úget_childrenr   N)Úhasattrrl   rš   Úiter_all_children)r/   ZskipContainersÚchildZ
grandchildr   r   r   rœ   M  s    rœ   c                 C   s    |   ¡ \}}| ¡ }|||dœS )N)Úhandlesrw   rf   )Zget_legend_handles_labelsrd   )r˜   Zlegendrž   rw   rf   r   r   r   Úget_legend_properties_  s    rŸ   c                 C   sV   | j }t ¡ }| ¡ }| |  ¡ ¡ |  |¡ | |¡ | d¡ t | 	¡ ¡ 
d¡S )a$  
    Convert a matplotlib image to a base64 png representation

    Parameters
    ----------
    image : matplotlib image object
        The image to be converted.

    Returns
    -------
    image_base64 : string
        The UTF8-encoded base64 string representation of the png image.
    r   zutf-8)r~   ÚioÚBytesIOrj   Z
get_extentZ	write_pngÚseekÚbase64Ú	b64encodeÚreadÚdecode)Úimager˜   Zbinary_bufferr   r   r   r   Úimage_to_base64e  s    


r¨   )NF)T)F)'Ú__doc__rB   r    r£   Znumpyr   r-   ri   Zmatplotlib.colorsr   Zmatplotlib.pathr   Zmatplotlib.markersr   Zmatplotlib.transformsr   r   r   r    r,   r1   ZLINETOZMOVETOZCURVE3ZCURVE4r7   r8   rF   rR   rU   rZ   r^   rx   rs   r{   r™   rœ   rŸ   r¨   r   r   r   r   Ú<module>   sZ   ûÿ     û	
&
:8
