a
    f1b2                     @   s   d Z ddlZddlmZ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 ddlmZ dd	lmZ G d
d dejZG dd dejZG dd deZG dd dejZG dd dZeedZeeejZe eZ!dS )z/
An experimental support for curvilinear grid.
    N)_apicbook)Path)host_axes_class_factory   )	axislinesgrid_helper_curvelinear)
AxisArtist)ExtremeFinderSimplec                   @   s   e Zd ZdS )FloatingAxisArtistHelperN)__name__
__module____qualname__ r   r   Dlib/python3.9/site-packages/mpl_toolkits/axisartist/floating_axes.pyr      s   r   c                       s6   e Zd Zd
 fdd	Zdd Zdd Zdd	 Z  ZS )FixedAxisArtistHelperNc                    sJ   | |\}}t j||||d |du r.|}|| _|| _|| _|| _dS )z}
        nth_coord = along which coordinate value varies.
         nth_coord = 0 ->  x axis, nth_coord = 1 -> y axis
        axis_directionN)get_data_boundarysuper__init__nth_coord_ticksvaluegrid_helper_side)selfr   sider   r   	nth_coord	__class__r   r   r      s    zFixedAxisArtistHelper.__init__c                 C   s   | j | | j j| _d S N)r   
update_lim
_grid_info)r   axesr   r   r   r!   ,   s    z FixedAxisArtistHelper.update_limc                    s  j jjd \}}}jd \}}}t|t| }}|durX|| }d| }	n|}d}	|durz|| }
d| }n|}
d}j j}t|dd \}}t|dd \}} fdd}jdkr||k||k@ }|| }t|j	}
||
|\	|
j
td	d
}||
| |k  |8  < |||\
||| |\|j
td	d
}|||	 |k  |	8  < ||
|\||
||	 \jd dd t|D njdkr||
k|
|k@ }|
| }
t|
j	}||
|\	|j
td	d
}|||	 |k  |	8  < ||
|\
||
||	 \|
j
td	d
}||
| |k  |8  < |||\||| |\jd dd t|D  	
fdd}| tg fS )z9tick_loc, tick_angle, tick_label, (optionally) tick_labellat_infolon_infoNgMbP?   c                    s$      j }|t| |gjS r    )get_transform	transData	transformnpcolumn_stackT)xyZtrf)r#   grid_finderr   r   transform_xyK   s    z>FixedAxisArtistHelper.get_tick_iterators.<locals>.transform_xyr   T)copy
lat_labelsc                 S   s   g | ]\}}|r|qS r   r   .0lmr   r   r   
<listcomp>`       z<FixedAxisArtistHelper.get_tick_iterators.<locals>.<listcomp>r   
lon_labelsc                 S   s   g | ]\}}|r|qS r   r   r3   r   r   r   r7   s   r8   c                  3   s  t 
	  } t   }
	 dk dk@ }|| t jd  | |<   j }t| |D ]\}}}}}|||f}	d}
d|
 |	d   krd|
 krvn qvd|
 |	d   krd|
 krvn qvt ||g\}}||g|||fV  qvd S )Nr   r&   gh㈵>r   )r*   Zarctan2ZpiZget_tick_transform	transAxeszipr)   Zrad2deg)ZddZdd2ZmmZtick_to_axesr-   r.   dZd2ZlabZc2ZdeltaZd1)r#   labelsr   xx1xx1axx1bxx2axx2byy1yy1ayy1byy2ayy2br   r   f1u   s    Hz4FixedAxisArtistHelper.get_tick_iterators.<locals>.f1)r   r/   r"   r*   asarray	_extremessortedr   	full_liker   Zastypefloatr;   iter)r   r#   lat_levslat_n
lat_factorlon_levslon_n
lon_factoryy0Zdyxx0Zdxextremesxminxmaxyminymaxr0   maskZxx00Zyy00rH   r   )r#   r/   r=   r   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   get_tick_iterators0   sd    



$z(FixedAxisArtistHelper.get_tick_iteratorsc                 C   sH   |  | tddddd| j \}}| j| | \}}tt||gS )N)
lon_lines0r   )r^   r   )
lat_lines0r   )r_   r   leftrightbottomtop)r!   dictr   r"   r   r*   r+   )r   r#   kvxxyyr   r   r   get_line   s    
zFixedAxisArtistHelper.get_line)N)r   r   r   r   r!   r]   rj   __classcell__r   r   r   r   r      s   Ur   c                   @   s   e Zd Zdd Zdd ZdS )ExtremeFinderFixedc                 C   s
   || _ dS )z
        This subclass always returns the same bounding box.

        Parameters
        ----------
        extremes : (float, float, float, float)
            The bounding box that this helper always returns.
        NrJ   )r   rW   r   r   r   r      s    	zExtremeFinderFixed.__init__c                 C   s   | j S r    rm   )r   r0   x1y1x2y2r   r   r   __call__   s    zExtremeFinderFixed.__call__N)r   r   r   r   rr   r   r   r   r   rl      s   rl   c                       sT   e Zd Zd fdd	Zdd ZdddZdd	 ZdddZe	ddd Z
  ZS )GridHelperCurveLinearNc                    s*   || _ t|}t j||||||d d S )N)grid_locator1grid_locator2tick_formatter1tick_formatter2)rJ   rl   r   r   )r   Z	aux_transrW   rt   ru   rv   rw   extreme_finderr   r   r   r      s    zGridHelperCurveLinear.__init__c                 C   s2   | j \}}}}t|df|df|df|dfd| S )z$
        Return v=0, nth=1.
        r   r   r`   )rJ   re   )r   r   Zlon1Zlon2Zlat1Zlat2r   r   r   r      s    z'GridHelperCurveLinear.get_data_boundaryc                 C   sV   |d u r| j }|d u r|}t| ||d}t|||d}|jd |j|j j |S )N)r   r   T)r#   r   r	   lineZset_clip_onZset_clip_boxbbox)r   Zlocr   r   offsetr#   Z_helperZaxisliner   r   r   new_fixed_axis   s    z$GridHelperCurveLinear.new_fixed_axisc                 C   s  | j d u rt | _ | j }| j}||j||||}t|d d \}}	t|dd  \}
}|||	\}}}||
|\}}}||	|
|f|d< |||f|d< |||f|d< |d|||d< |	d|||d< |d u rt
|d | }nt
|d | | }|d u rt
|d | }nt
|d | | }||||k ||	k @  ||
|k ||k @  ||	|
|\}}||d< ||d	< |j|d d |dd  g|R  \}}||d
< ||d< d S )Nr&   rW   r%   r$   rc   r9   r2   	lon_lines	lat_linesr^   r_   )r"   re   r/   rx   Zinv_transform_xyrK   rt   ru   rv   rw   r*   rI   Z_get_raw_grid_lines)r   rn   ro   rp   rq   Z	grid_infor/   rW   Zlon_minZlon_maxZlat_minZlat_maxrR   rS   rT   rO   rP   rQ   Z
lon_valuesZ
lat_valuesr}   r~   r   r   r   _update_grid   sX    



z"GridHelperCurveLinear._update_gridmajorbothc                 C   s8   g }|dv r| | jd  |dv r4| | jd  |S )N)r   r-   r}   )r   r.   r~   )extendr"   )r   ZwhichZaxisZ
grid_linesr   r   r   get_gridlines  s    z#GridHelperCurveLinear.get_gridlinesz3.5c              	   C   s   | j \}}}}| j}t||d}t||}t||}t||d}	t|	|}
t|	|}t|dd |dd |ddd |
g}t|dd |	dd |dd |	ddd g}|t||g }|S )zK
        Return (N, 2) array of (x, y) coordinate of the boundary.
        d   Nr   )	rJ   Z
_aux_transr*   ZlinspacerL   Zconcatenater)   ZarrayZ	transpose)r   x0rn   y0ro   Ztrrh   rU   rC   ri   rV   r>   ZxxxZyyytr   r   r   get_boundary"  s    ,4z"GridHelperCurveLinear.get_boundary)NNNN)NNNN)r   r   )r   r   r   r   r   r|   r   r   r   Z
deprecatedr   rk   r   r   r   r   rs      s           
(5
rs   c                       s8   e Zd Z fddZdd Z fddZdd Z  ZS )	FloatingAxesBasec                    sV   | dd }|d u rtdt|ds.tdt j|i | | d |   d S )Nr   z*FloatingAxes requires grid_helper argumentr   z.grid_helper must implement get_boundary methodg      ?)get
ValueErrorhasattrr   r   Z
set_aspectadjust_axes_lim)r   argskwargsr   r   r   r   r   :  s    

zFloatingAxesBase.__init__c                 C   s\   t |  jg d\\}}\}}\}}\}}t||f||f||f||fg}d| _|S )Nr`   r   )mapget_grid_helperr   mpatchesZPolygonget_pathZ_interpolation_steps)r   r   _rn   r   ro   patchr   r   r   _gen_axes_patchD  s    "
z FloatingAxesBase._gen_axes_patchc                    sd   t    | j|  j | j  t   }|	| j
 || j | j| | j| d S r    )r   clar   Zset_transformr   r/   r'   r(   r   Z
set_figureZfigurer:   Zset_clip_pathZ	gridlines)r   Z
orig_patchr   r   r   r   N  s    

zFloatingAxesBase.clac                 C   sL   | j  | j  | j }|dd}| |j|j | 	|j
|j d S )NgRQ?)r   r   Zget_extentsr'   r(   ZexpandedZset_xlimrX   rY   Zset_ylimrZ   r[   )r   rz   r   r   r   r   [  s    
z FloatingAxesBase.adjust_axes_lim)r   r   r   r   r   r   r   rk   r   r   r   r   r   8  s   

r   z
Floating{})"__doc__Znumpyr*   Z
matplotlibr   r   Zmatplotlib.patchesZpatchesr   Zmatplotlib.pathr   Zmatplotlib.axesr#   ZmaxesZ%mpl_toolkits.axes_grid1.parasite_axesr    r   r   Zaxis_artistr	   r/   r
   r   r   rl   rs   r   Z_make_class_factoryZfloatingaxes_class_factoryZAxesZFloatingAxesZsubplot_class_factoryZFloatingSubplotr   r   r   r   <module>   s0   
t ,