B
    ]t\BV                 @   s   d 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lmZ ddlmZmZ G d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd dejZeeZG dd deZeeZdS )a  
Axislines includes modified implementation of the Axes class. The
biggest difference is that the artists responsible for drawing the axis spine,
ticks, ticklabels and axis labels are separated out from mpl's Axis
class. Originally, this change was motivated to support curvilinear
grid. Here are a few reasons that I came up with a new axes class:


 * "top" and "bottom" x-axis (or "left" and "right" y-axis) can have
   different ticks (tick locations and labels). This is not possible
   with the current mpl, although some twin axes trick can help.

 * Curvilinear grid.

 * angled ticks.

In the new axes class, xaxis and yaxis is set to not visible by
default, and new set of artist (AxisArtist) are defined to draw axis
line, ticks, ticklabels and axis label. Axes.axis attribute serves as
a dictionary of these artists, i.e., ax.axis["left"] is a AxisArtist
instance responsible to draw left y-axis. The default Axes.axis contains
"bottom", "left", "top" and "right".

AxisArtist can be considered as a container artist and
has following children artists which will draw ticks, labels, etc.

 * line
 * major_ticks, major_ticklabels
 * minor_ticks, minor_ticklabels
 * offsetText
 * label

Note that these are separate artists from Axis class of the
original mpl, thus most of tick-related command in the original mpl
won't work, although some effort has made to work with. For example,
color and markerwidth of the ax.axis["bottom"].major_ticks will follow
those of Axes.xaxis unless explicitly specified.

In addition to AxisArtist, the Axes will have *gridlines* attribute,
which obviously draws grid lines. The gridlines needs to be separated
from the axis as some gridlines can never pass any axis.

    N)rcParams)Path)Bbox   )AxislineStyle)
AxisArtistGridlinesCollectionc               @   s@   e Zd ZdZG dd deZG dd deZG dd deZdS )	AxisArtistHelperae  
    AxisArtistHelper should define
    following method with given APIs. Note that the first axes argument
    will be axes attribute of the caller artist.::


        # LINE (spinal line?)

        def get_line(self, axes):
            # path : Path
            return path

        def get_line_transform(self, axes):
            # ...
            # trans : transform
            return trans

        # LABEL

        def get_label_pos(self, axes):
            # x, y : position
            return (x, y), trans


        def get_label_offset_transform(self,                 axes,
                pad_points, fontprops, renderer,
                bboxes,
                ):
            # va : vertical alignment
            # ha : horizontal alignment
            # a : angle
            return trans, va, ha, a

        # TICK

        def get_tick_transform(self, axes):
            return trans

        def get_tick_iterators(self, axes):
            # iter : iterable object that yields (c, angle, l) where
            # c, angle, l is position, tick angle, and label

            return iter_major, iter_minor


    c               @   s    e Zd ZdZdd Zdd ZdS )zAxisArtistHelper._BasezBase class for axis helper.c             C   s   d\| _ | _d S )N)gh㈵>gh㈵>)delta1delta2)self r   @lib/python3.7/site-packages/mpl_toolkits/axisartist/axislines.py__init__l   s    zAxisArtistHelper._Base.__init__c             C   s   d S )Nr   )r   axesr   r   r   
update_limo   s    z!AxisArtistHelper._Base.update_limN)__name__
__module____qualname____doc__r   r   r   r   r   r   _Basej   s   r   c                   sb   e Zd ZdZedddddZd fdd	Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Z  ZS )zAxisArtistHelper.Fixedz7Helper class for a fixed (in the axes coordinate) axis.)r   r   )r   r   )r   r   )leftrightbottomtopNc                s   || _ |dkrtd| |dkr<|dkr0d}n|dkr<d}|| _t   | j| | _tddgd	d	gg}d| }| j| |dd|f< t	|| _
dS )
z
            nth_coord = along which coordinate value varies
            in 2d, nth_coord = 0 ->  x axis, nth_coord = 1 -> y axis
            )r   r   r   r   z%sN)r   r   r   )r   r   r   g        g      ?)_loc
ValueError	nth_coordsuperr   _default_passthru_ptpassthru_ptnparrayr   _path)r   locr   _vertsfixed_coord)	__class__r   r   r   z   s     

zAxisArtistHelper.Fixed.__init__c             C   s   | j S )N)r   )r   r   r   r   get_nth_coord   s    z$AxisArtistHelper.Fixed.get_nth_coordc             C   s   | j S )N)r#   )r   r   r   r   r   get_line   s    zAxisArtistHelper.Fixed.get_linec             C   s   |j S )N)	transAxes)r   r   r   r   r   get_line_transform   s    z)AxisArtistHelper.Fixed.get_line_transformc             C   s   |j S )N)r*   )r   r   r   r   r   get_axislabel_transform   s    z.AxisArtistHelper.Fixed.get_axislabel_transformc             C   s&   | j }tddddd| \}}||fS )z
            label reference position in transAxes.

            get_label_transform() returns a transform of (transAxes+offset)
            ))g        g      ?Z   ))g      ?g      ?r-   ))g      ?g        r   ))g      ?g      ?r   )r   r   r   r   )r   dict)r   r   r$   posangle_tangentr   r   r   get_axislabel_pos_angle   s    z.AxisArtistHelper.Fixed.get_axislabel_pos_anglec             C   s   |  | g| j }|S )N)Zget_xaxis_transformZget_yaxis_transformr   )r   r   
trans_tickr   r   r   get_tick_transform   s    z)AxisArtistHelper.Fixed.get_tick_transform)N)r   r   r   r   r.   r   r   r(   r)   r+   r,   r1   r3   __classcell__r   r   )r'   r   Fixedr   s   r5   c                   s,   e Zd Z fddZdd Zdd Z  ZS )zAxisArtistHelper.Floatingc                s   || _ || _t   d S )N)r   _valuer   r   )r   r   value)r'   r   r   r      s    z"AxisArtistHelper.Floating.__init__c             C   s   | j S )N)r   )r   r   r   r   r(      s    z'AxisArtistHelper.Floating.get_nth_coordc             C   s   t dd S )Nz6get_line method should be defined by the derived class)RuntimeError)r   r   r   r   r   r)      s    z"AxisArtistHelper.Floating.get_line)r   r   r   r   r(   r)   r4   r   r   )r'   r   Floating   s   r9   N)r   r   r   r   objectr   r5   r9   r   r   r   r   r	   9   s   /Lr	   c               @   s0   e Zd ZG dd dejZG dd dejZdS )AxisArtistHelperRectlinearc                   s&   e Zd Zd fdd	Zdd Z  ZS )z AxisArtistHelperRectlinear.FixedNc                s&   t  || |j|jg| j | _dS )z
            nth_coord = along which coordinate value varies
            in 2d, nth_coord = 0 ->  x axis, nth_coord = 1 -> y axis
            N)r   r   xaxisyaxisr   axis)r   r   r$   r   )r'   r   r   r      s    z)AxisArtistHelperRectlinear.Fixed.__init__c       	         s   j }|dkrd\ nd\ jj }j| fddt|D }jj }j| fddt|D }|}||j	
   fdd}||||||fS )	z tick_loc, tick_angle, tick_label)r   r   )r-   r   )r   r-   c                s   g | ]\}}  ||qS r   )	formatter).0ival)majorr   r   
<listcomp>   s    zGAxisArtistHelperRectlinear.Fixed.get_tick_iterators.<locals>.<listcomp>c                s   g | ]\}}  ||qS r   )r?   )r@   rA   rB   )minorr   r   rD      s    c             3   sr   xlt | |D ]^\}}tj}||j< |}dj |j   krXdj krn q| |fV  qW d S )Ng        g      ?)ziplistr    r   transform_pointr
   r   )locslabelsxlcc2)angle_normalr0   r   tr2axr   r   _f   s    


*z?AxisArtistHelperRectlinear.Fixed.get_tick_iterators.<locals>._f)r   r>   rC   locatorr?   set_locs	enumeraterE   r3   r*   inverted)	r   r   r$   	majorLocsmajorLabels	minorLocsminorLabelsr2   rQ   r   )rO   r0   rC   rE   r   rP   r   get_tick_iterators   s     

z3AxisArtistHelperRectlinear.Fixed.get_tick_iterators)N)r   r   r   r   rZ   r4   r   r   )r'   r   r5      s   
r5   c                   sN   e Zd Zd fdd	Zdd Zdd Zdd	 Zd
d Zdd Zdd Z	  Z
S )z#AxisArtistHelperRectlinear.Floatingr   c                s,   t  || || _|j|jg| j | _d S )N)r   r   _axis_directionr<   r=   r   r>   )r   r   r   Zpassingthrough_pointaxis_direction)r'   r   r   r     s    z,AxisArtistHelperRectlinear.Floating.__init__c             C   s^   t ddgddgg}d| j }|j|j  }|| j| jg}|| |d d |f< t|S )Ng        g      ?r   )	r!   r"   r   	transDatar*   rU   rH   r6   r   )r   r   r%   r&   trans_passingthrough_pointpr   r   r   r)     s    


z,AxisArtistHelperRectlinear.Floating.get_linec             C   s   |j S )N)r*   )r   r   r   r   r   r+     s    z6AxisArtistHelperRectlinear.Floating.get_line_transformc             C   s   |j S )N)r*   )r   r   r   r   r   r,     s    z;AxisArtistHelperRectlinear.Floating.get_axislabel_transformc             C   s   | j }| jdkrd}nd}ddg}d| j }|j|j  }|| j| jg}|| ||< d||   krrdksxn dS ||fS dS )	z
            label reference position in transAxes.

            get_label_transform() returns a transform of (transAxes+offset)
            r   r-   g      ?r   g        g      ?)NNN)r[   r   r]   r*   rU   rH   r6   )r   r   r$   Zangler%   r&   r^   r_   r   r   r   r1     s    


z;AxisArtistHelperRectlinear.Floating.get_axislabel_pos_anglec             C   s   |j S )N)r]   )r   r   r   r   r   r3   6  s    z6AxisArtistHelperRectlinear.Floating.get_tick_transformc                s   j }|dkrd\ nd\ jdkr4d\ nd\ jj }j| fddt|D }jj }j| fddt|D }|j	|j
   fdd	}||||||fS )
z tick_loc, tick_angle, tick_label)r   r   )r-   r   )r   r-   r   c                s   g | ]\}}  ||qS r   )r?   )r@   rA   rB   )rC   r   r   rD   M  s    zJAxisArtistHelperRectlinear.Floating.get_tick_iterators.<locals>.<listcomp>c                s   g | ]\}}  ||qS r   )r?   )r@   rA   rB   )rE   r   r   rD   R  s    c             3   s   xt | |D ]\}}jjg}||j< |\}}d|  krLdkrn qd|  krddkrn qdj ||gj   krdj krn q| |fV  qW d S )Nr   r   )rF   r6   r   rH   r
   r   )rI   rJ   rK   rL   rM   Zc1rN   )rO   r0   r   rP   r   r   rQ   V  s    
0zBAxisArtistHelperRectlinear.Floating.get_tick_iterators.<locals>._f)r[   r   r>   rC   rR   r?   rS   rT   rE   r]   r*   rU   )r   r   r$   rV   rW   rX   rY   rQ   r   )rO   r0   rC   rE   r   rP   r   rZ   9  s$    


z6AxisArtistHelperRectlinear.Floating.get_tick_iterators)r   )r   r   r   r   r)   r+   r,   r1   r3   rZ   r4   r   r   )r'   r   r9      s   r9   N)r   r   r   r	   r5   r9   r   r   r   r   r;      s   2r;   c                   sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )GridHelperBasec                s   d| _ d | _t   d S )NT)_force_update_old_limitsr   r   )r   )r'   r   r   r   g  s    zGridHelperBase.__init__c             C   sX   |  \}}| \}}| js0| j||||fkrT| |||| d| _||||f| _d S )NF)get_xlimget_ylimra   rb   _update)r   r   x1x2y1y2r   r   r   r   l  s    zGridHelperBase.update_limc             C   s   d S )Nr   )r   rf   rg   rh   ri   r   r   r   re   u  s    zGridHelperBase._updatec             C   s
   d| _ d S )NT)ra   )r   r   r   r   
invalidatex  s    zGridHelperBase.invalidatec             C   s   | j  S )N)ra   )r   r   r   r   valid{  s    zGridHelperBase.validc             C   s   g S )z
        Return list of grid lines as a list of paths (list of points).

        *which* : "major" or "minor"
        *axis* : "both", "x" or "y"
        r   )r   whichr>   r   r   r   get_gridlines~  s    zGridHelperBase.get_gridlinesc             C   sD   t d|jtd td td d}|| ||  |j| |S )z
        Create and return a new GridlineCollection instance.

        *which* : "major" or "minor"
        *axis* : "both", "x" or "y"

        Nz
grid.colorzgrid.linestylezgrid.linewidth)	transformZcolorsZ
linestylesZ
linewidths)r   r]   r   Z_set_artist_propsZset_grid_helperr   )r   Zax	gridlinesr   r   r   new_gridlines  s    

zGridHelperBase.new_gridlines)r   r   r   r   r   re   rj   rk   rm   rp   r4   r   r   )r'   r   r`   e  s   		r`   c                   s:   e Zd Z fddZdddZdddZdddZ  ZS )GridHelperRectlinearc                s   t    || _d S )N)r   r   r   )r   r   )r'   r   r   r     s    
zGridHelperRectlinear.__init__Nc             C   sF   |d krt d | j}t|||}|d kr2|}t||||d}|S )Nz6'new_fixed_axis' explicitly requires the axes keyword.)offsetr\   )warningswarnr   r;   r5   r   )r   r$   r   r\   rr   r   _helperaxisliner   r   r   new_fixed_axis  s    
z#GridHelperRectlinear.new_fixed_axisr   c       	      C   s`   |d krt d | j}||f}|j}t||||}t||}|jd |j	|jj
 |S )Nz9'new_floating_axis' explicitly requires the axes keyword.T)rs   rt   r   r]   r;   r9   r   lineZset_clip_onZset_clip_boxZbbox)	r   r   r7   r\   r   Zpassthrough_pointrn   ru   rv   r   r   r   new_floating_axis  s    
z&GridHelperRectlinear.new_floating_axisrC   bothc             C   s   g }|dkrzg }| j  \}}|dkr:|| j jj  |dkrV|| j jj  x"|D ]}|||g||gg q\W |dkr| j  \}}	g }| j j	j
r|| j j	j  | j j	jr|| j j	j  x"|D ]}
|||	g|
|
gg qW |S )z
        return list of gridline coordinates in data coordinates.

        *which* : "major" or "minor"
        *axis* : "both", "x" or "y"
        )rz   rK   )rz   rC   )rz   rE   )rz   y)r   rd   extendr<   rC   rR   rE   appendrc   r=   _gridOnMajor_gridOnMinor)r   rl   r>   ro   rI   rh   ri   rK   rf   rg   r{   r   r   r   rm     s(    



z"GridHelperRectlinear.get_gridlines)NNNN)r   N)rC   rz   )r   r   r   r   rw   ry   rm   r4   r   r   )r'   r   rq     s      
 
rq   c               @   s$   e Zd Zdd Zdd Zdd ZdS )SimpleChainedObjectsc             C   s
   || _ d S )N)_objects)r   Zobjectsr   r   r   r     s    zSimpleChainedObjects.__init__c                s   t  fdd| jD }|S )Nc                s   g | ]}t | qS r   )getattr)r@   a)kr   r   rD     s    z4SimpleChainedObjects.__getattr__.<locals>.<listcomp>)r   r   )r   r   Z_ar   )r   r   __getattr__  s    z SimpleChainedObjects.__getattr__c             O   s   x| j D ]}||| qW d S )N)r   )r   Zklkwargsmr   r   r   __call__  s    zSimpleChainedObjects.__call__N)r   r   r   r   r   r   r   r   r   r   r     s   r   c                   s   e Zd ZG dd deZdd fdd
Zd"ddZd#d	d
Zedd Z	d$ddZ
d%ddZ fddZdd Zd& fdd	Z fddZdd Zd'ddZd(d d!Z  ZS ))Axesc                   s,   e Zd Z fddZdd Zdd Z  ZS )zAxes.AxisDictc                s   || _ t   d S )N)r   r   r   )r   r   )r'   r   r   r     s    zAxes.AxisDict.__init__c                s`   t |tr t fdd|D S t |trP|td krFtt  S tdnt |S d S )Nc                s   g | ]}t  |qS r   )r.   __getitem__)r@   Zk1)r   r   r   rD     s    z-Axes.AxisDict.__getitem__.<locals>.<listcomp>zUnsupported slice)	
isinstancetupler   slicerG   valuesr   r.   r   )r   r   r   )r   r   r     s    


zAxes.AxisDict.__getitem__c             O   s   t jj| jf||S )N)maxesr   r>   r   )r   argsr   r   r   r   r     s    zAxes.AxisDict.__call__)r   r   r   r   r   r   r4   r   r   )r'   r   AxisDict   s   r   N)grid_helperc               s4   d| _ |r|nt| | _t j|| | d d S )NT)_axisline_onrq   _grid_helperr   r   toggle_axisline)r   r   r   r   )r'   r   r   r     s
    
zAxes.__init__c             C   s   |d kr| j  }|rRd| _ x| j D ]}|d q&W | jd | jd n<d| _ x| j D ]}|d qdW | jd | jd d S )NTF)r   Zspinesr   set_visibler<   r=   )r   bsr   r   r   r     s    zAxes.toggle_axislinec             C   s   |d kr| }|  | | _|  j}x dD ]}||||d| j|< q(W x@| jd | jd gD ](}|jd |jd |jd qZW d S )N)r   r   r   r   )r$   r   r\   r   r   F)r   
_axislinesget_grid_helperrw   Zlabelr   Zmajor_ticklabelsZminor_ticklabels)r   r   rw   r$   rv   r   r   r   _init_axis_artists+  s    

zAxes._init_axis_artistsc             C   s   | j S )N)r   )r   r   r   r   r>   :  s    z	Axes.axisc             C   s   |dkr|   }|| }|S )z
        Create and return a new GridlineCollection instance.

        *which* : "major" or "minor"
        *axis* : "both", "x" or "y"

        N)r   rp   )r   r   ro   r   r   r   rp   >  s    
zAxes.new_gridlinesc             C   s   |  || _d S )N)rp   ro   )r   r   r   r   r   _init_gridlinesL  s    zAxes._init_gridlinesc                s.   |    t   | j| jj |   d S )N)r   r   claro   set_clip_pathr   patchr   )r   )r'   r   r   r   P  s    
zAxes.clac             C   s   | j S )N)r   )r   r   r   r   r   [  s    zAxes.get_grid_helperrC   rz   c                s   t  j|f||d| | js$dS |dkr^| jjjsT| jjjsT| jjjsT| jjjrZd}nd}| j	| | j
| | j| t|rtj| jf| dS )zW
        Toggle the gridlines, and optionally set the properties of the lines.
        )rl   r>   NTF)r   gridr   r   r<   r   r~   r=   ro   Z	set_whichZset_axisr   lenmartistZsetp)r   r   rl   r>   r   )r'   r   r   r   ^  s    z	Axes.gridc                s2   | j r| j | jf}ng }|t   |S )N)r   r   r   ro   r|   r   get_children)r   Zchildren)r'   r   r   r   y  s
    zAxes.get_childrenc             C   s   | j   d S )N)r   rj   )r   r   r   r   invalidate_grid_helper  s    zAxes.invalidate_grid_helperc             C   s    |   }|j|d d || d}|S )N)r   r\   rr   r   )r   rw   )r   r$   rr   ghr>   r   r   r   rw     s    zAxes.new_fixed_axisr   c             C   s   |   }|j|||| d}|S )N)r\   r   )r   ry   )r   r   r7   r\   r   r>   r   r   r   ry     s
    zAxes.new_floating_axis)N)N)N)N)NrC   rz   )N)r   )r   r   r   r.   r   r   r   r   propertyr>   rp   r   r   r   r   r   r   rw   ry   r4   r   r   )r'   r   r     s   




r   c                   s   e Zd Z fddZ  ZS )AxesZeroc                sz   t    | jj}|ddd| d}|j| j |d || jd< |ddd| d}|j| j |d || jd	< d S )
Nr   g        r   )r   r7   r\   r   FZxzeror   r   Zyzero)	r   r   r   ry   rx   r   r   r   r   )r   ry   Z
xaxis_zeroZ
yaxis_zero)r'   r   r   r     s     



zAxesZero._init_axis_artists)r   r   r   r   r4   r   r   )r'   r   r     s   r   ) r   rs   Znumpyr!   Z
matplotlibr   Zmatplotlib.artistZartistr   Zmatplotlib.axesr   r   Zmatplotlib.pathr   Zmatplotlib.transformsr   Zaxisline_styler   Zaxis_artistr   r   r:   r	   r;   r`   rq   r   r   Zsubplot_class_factoryZSubplotr   ZSubplotZeror   r   r   r   <module>+   s*     9S 
