U
    td                     @   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   bottomZ   )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
 nd\}	}
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  |st	|j
D ]t\}\}}|| \}}|| \}}|||j  \}}tjj||g||g||g||ggfddd|}|| qRnt	|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   
