U
    Ñtœd…  ã                   @   sJ   d Z ddlmZ ddlmZ ddlmZ dZeƒ \ZZ	G dd„ deƒZ
d	S )
za
Drawing routines to draw the matrices.

This module provides implementations of matrix drawers.
é    )ÚAbstractDrawer)Ústr_to_orientationé   )Úfind_matplotlib)ÚMatplotlibDendrogramDrawerc                       s2   e Zd ZdZ‡ fdd„Zdd„ Zd	dd„Z‡  ZS )
r   z)Matplotlib drawer object for dendrograms.c                    s   t ƒ  ¡  || _dS )zvConstructs the drawer and associates it to the given Axes.

        @param ax: the Axes on which we will draw
        N)ÚsuperÚ__init__Úcontext)ÚselfÚax©Ú	__class__© ú]/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/matplotlib/dendrogram.pyr      s    
z#MatplotlibDendrogramDrawer.__init__c           
      C   s‚   |j dks|j | dkrdS |dkr0d\}}}	n2|dkrDd\}}}	n|dkrXd\}}}	n
d\}}}	|j|||j | |||	d	� dS )
a9  Plots a dendrogram item to the given Cairo context

        @param dendro: the dendrogram object
        @param orientation: whether the dendrogram is horizontally oriented
        @param idx: the index of the item
        @param x: the X position of the item
        @param y: the Y position of the item
        NÚlr)ÚrightÚcenterr   Úrl)Úleftr   r   Útb)r   ÚbottoméZ   )r   Útopr   )ÚhaÚvaÚrotation)Ú_namesÚtext)
r
   Údendror   ÚorientationÚidxÚxÚyr   r   r   r   r   r   Ú
_plot_item   s"    	
úz%MatplotlibDendrogramDrawer._plot_itemr   c                 K   sV  ddl m} | j}| dd¡ | dd¡|d< d|krFd	|krFd
|d	< |jdkrfdd„ t|jƒD ƒ|_t|dd�}|dk}|dg|j dd�}| ¡ }|�s"d\}	}
t	|ƒD ]\}}|	df||< |	d
7 }	q¬|j
D ]:\}}|| d || d  d }	|
d
7 }
| |	|
f¡ qÐ|dk�rª| d
¡ nˆd\}	}
t	|ƒD ]\}}d|
f||< |
d
7 }
�q2|j
D ]<\}}|| d
 || d
  d }
|	d
7 }	| |	|
f¡ �qX|dk�rª| d¡ g g  }}t|jƒD ]>}|| \}	}
|�sà| |	¡ n
| |
¡ | |j| ¡ �q¾|�s$| |¡ | |¡ | g ¡ n| |¡ | |¡ | g ¡ |�sÊt	|j
ƒD ]t\}\}}|| \}}|| \}}|||j  \}}tjj||g||g||g||ggfdddœ|—Ž}| |¡ �qRn€t	|j
ƒD ]t\}\}}|| \}}|| \}}|||j  \}}tjj||g||g||g||ggfdddœ|—Ž}| |¡ �qÔ| ¡  dS )aÆ  Draws the given Dendrogram in a matplotlib Axes.

        Other keyword arguments are passed to mpl.patches.Polygon.

        @param dendro: the igraph.Dendrogram to plot.
        @param orientation: the direction of the plot. Accepted values are "lr"
          (root on the right), "rl" (root on the left), "tb" (root at the bottom),
          and "bt" (root at the top). A few aliases are available (see
          L{utils.str_to_orientation}).
        r   )ÚLayoutÚpaletteNÚcolorÚblackZ	edgecolorZlwZ	linewidthr   c                 S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0r!   r   r   r   Ú
<listcomp>U   s     z3MatplotlibDendrogramDrawer.draw.<locals>.<listcomp>T)Zreversed_vertical)r   r   )r   r   é   )Údimg       @Zbtr   FÚnone)ÚclosedZ	facecolor)Zigraph.layoutr$   r	   Úpopr   ÚrangeZ_nitemsr   Z_traverse_inorderÚ	enumerateZ_mergesÚappendZmirrorZ
set_xticksZset_xticklabelsZ
set_yticksZset_yticklabelsÚmplZpatchesZPolygonZ	add_patchZautoscale_view)r
   r   r   Úkwdsr$   r   ZhorizZlayoutZinorderr!   r"   r    ÚelementZid1Zid2ZticksZ
ticklabelsZx0Zy0Úx1Úy1Zx2Úy2Zpolyr   r   r   Údraw<   s–    











ÿýüÿýüzMatplotlibDendrogramDrawer.draw)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r9   Ú__classcell__r   r   r   r   r      s    r   N)r=   Zigraph.drawing.baseclassesr   Zigraph.drawing.utilsr   Úutilsr   Ú__all__r3   Ú_r   r   r   r   r   Ú<module>   s   
