o
    i7                     @   s   d Z ddlmZ ddlZddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZ dd	lmZmZ G d
d dejZG dd dejZG dd deZdS )z/
An experimental support for curvilinear grid.
    )chainN)Path)Affine2DIdentityTransform   )AxisArtistHelperGridHelperBase)
AxisArtist)
GridFinder_deprecate_factor_nonec                       sD   e Zd ZdZd fdd	Zdd ZdddZd	d
 Zdd Z  Z	S )FixedAxisArtistHelperz(
    Helper class for a fixed axis.
    Nc                    s8   t  j|d || _|du r| j}|| _|| _d| _dS )}
        nth_coord = along which coordinate value varies.
         nth_coord = 0 ->  x axis, nth_coord = 1 -> y axis
        )locNF)super__init__grid_helper	nth_coordnth_coord_ticksside_limits_inverted)selfr   r   r   	__class__ j/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/mpl_toolkits/axisartist/grid_helper_curvelinear.pyr      s   
zFixedAxisArtistHelper.__init__c                 C   sL   | j | | jdkr| \}}n| \}}||kr!d| _d S d| _d S )Nr   TF)r   
update_limr   get_ylimget_xlimr   )r   axesxy1xy2r   r   r   r   $   s   


z FixedAxisArtistHelper.update_limc                 C   s2   |d u rd| j  | _ d S |dv r|| _ d S td)Nr   )r   r   zwrong coord number)r   	Exception)r   coord_numberr   r   r   change_tick_coord1   s
   
z'FixedAxisArtistHelper.change_tick_coordc                 C      |j S N	transDatar   r   r   r   r   get_tick_transform9      z(FixedAxisArtistHelper.get_tick_transformc                 C   s^   | j }| jrddddd| j }n| j}|| j|}|jd| j |dd}t||tg fS )	z tick_loc, tick_angle, tick_labelrightleftbottomtop)r,   r+   r.   r-   r   T)minor)r   r   r   get_tick_iteratorr   r   iter)r   r   gr   ti1ti2r   r   r   get_tick_iterators<   s   z(FixedAxisArtistHelper.get_tick_iteratorsr%   )
__name__
__module____qualname____doc__r   r   r#   r)   r5   __classcell__r   r   r   r   r      s    
r   c                       s^   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	dd Z
dd Z  ZS )FloatingAxisArtistHelperNc                    s:   t  || || _|| _tj tjf| _d| _d| _dS )r   Nd   )	r   r   valuer   npinf	_extremes_get_line_path_line_num_points)r   r   r   r=   axis_directionr   r   r   r   O   s   
z!FloatingAxisArtistHelper.__init__c                 C   s,   |d u rt j }|d u rt j}||f| _d S r%   )r>   r?   r@   )r   e1e2r   r   r   set_extremes]   s
   z%FloatingAxisArtistHelper.set_extremesc              	   C   s  | j | | \}}| \}}| j j}||j||||}|\}}	}
}| j\}}| jdkr;t	||
}
t
||}n| jdkrJt	||}t
||	}	|||	\}}}||
|\}}}| jdkr~t| j| jt| j}t|
|| j}|||\}}n!| jdkrt||	| j}t| j| jt| j}|||\}}||	|
|f||t|f||t|f|dt|||dt||||fd| _d S )Nr   r   r-   )extremeslon_infolat_info
lon_labels
lat_labelsline_xy)r   r   r   r   grid_finderextreme_finderinv_transform_xyr@   r   maxmingrid_locator1grid_locator2r>   fullrB   r=   typelinspacetransform_xyr   tick_formatter1tick_formatter2	grid_info)r   r   x1x2y1y2rM   rG   lon_minlon_maxlat_minlat_maxe_mine_maxlon_levslon_n
lon_factorlat_levslat_n
lat_factorxx0yy0xxyyr   r   r   r   d   sL   












z#FloatingAxisArtistHelper.update_limc                 C      t  S r%   )r   r(   r   r   r   get_axislabel_transform   r*   z0FloatingAxisArtistHelper.get_axislabel_transformc                 C   sp  | j d }| jdkr&| j}|d |d  d }d}t|d |d  d }n | jdkrF|d |d  d }| j}t|d |d  d }d}| jj}||g|g\\}\}	|j|j }
|
	||	g}d|d   krodkrn dS d|d   kr~dkrn dS |j	||	g\}}||| g|| g\\}\}|j	||g\}}||ft
t
|| || fS dS )NrG   r         i  r   )NN)rZ   r   r=   absr   rM   rW   r'   	transAxes	transformr>   rad2degarctan2)r   r   rG   rk   rl   dxxdyyrM   xx1yy1data_to_axespxx1cyy1cxx2yy2xx2cyy2cr   r   r   get_axislabel_pos_angle   s0   


 "z0FloatingAxisArtistHelper.get_axislabel_pos_anglec                 C   ro   r%   )r   r(   r   r   r   r)      r*   z+FloatingAxisArtistHelper.get_tick_transformc                    s  j jjd \}}}t|}|t| }dt| }jd \}}}	t|}|t|	 }
dt|	 }djv rAj\}}ntj\}}|du rPtj }|du rWtj}j	dkri||k||k@ }|| }nj	dkrz||
k|
|k@ }|
| }
 fdd}j	dkrt
|j}
||
|\	|
 }||
| |k  |8  < |||\
||| |\||
|\||
|| \jd	 d
d t|D nSj	dkr't
|
j}||
|\	||
|\
||
|| \|
 }||
| |k  |8  < |||\||| |\jd dd t|D  	
fdd}| tg fS )z9tick_loc, tick_angle, tick_label, (optionally) tick_labelrI   g{Gz?rH   Nr   r   c                    s>    | |\}} jt||g }| \}}||fS r%   )rW   r'   ru   r>   array	transpose)xyr[   r]   x2y2r\   r^   )r   rM   r   r   rW      s   zAFloatingAxisArtistHelper.get_tick_iterators.<locals>.transform_xyrK   c                 S      g | ]\}}|r|qS r   r   .0lmr   r   r   
<listcomp>       z?FloatingAxisArtistHelper.get_tick_iterators.<locals>.<listcomp>rJ   c                 S   r   r   r   r   r   r   r   r      r   c                  3   s   t 
	  } t   }
	kk@ }|| t jd  | |<   j }t| |D ]F\}}}}}|||f}	d}
d|
 |	d   krXd|
 kr~n q8d|
 |	d   krjd|
 kr~n q8t ||g\}}||g|||fV  q8d S )Nrq   gh㈵>r   r   )r>   rw   pir)   rt   zipru   rv   )dddd2mmtick_to_axesr   r   dd2labc2deltad1)r   labelsr   rz   xx1axx1bxx2axx2br{   yy1ayy1byy2ayy2br   r   f1   s   Hz7FloatingAxisArtistHelper.get_tick_iterators.<locals>.f1)r   rM   rZ   r>   asarrayr   r@   sortedr?   r   	full_liker=   copyr   r1   )r   r   rh   ri   rj   rl   dyre   rf   rg   rk   dxe0rD   maskrW   xx00r   r   )r   rM   r   r   rz   r   r   r   r   r{   r   r   r   r   r   r5      s^   








$z+FloatingAxisArtistHelper.get_tick_iteratorsc                 C   r$   r%   r&   r(   r   r   r   get_line_transform
  r*   z+FloatingAxisArtistHelper.get_line_transformc                 C   sB   |  | | jd \}}| jd u rtt||gS | |||S )NrL   )r   rZ   rA   r   r>   column_stack)r   r   r   r   r   r   r   get_line  s
   

z!FloatingAxisArtistHelper.get_liner%   )r6   r7   r8   r   rF   r   rp   r   r)   r5   r   r   r:   r   r   r   r   r;   M   s    +Xr;   c                       sv   e Zd Z					d fdd	ZdddZdd Z				ddd	Z		
dddZdd ZdddZ	dddZ
  ZS )GridHelperCurveLinearNc                    s4   t    d| _d| _|| _t||||||| _dS )a  
        aux_trans : a transform from the source (curved) coordinate to
        target (rectilinear) coordinate. An instance of MPL's Transform
        (inverse transform should be defined) or a tuple of two callable
        objects which defines the transform and its inverse. The callables
        need take two arguments of array of source coordinates and
        should return two target coordinates.

        e.g., ``x2, y2 = trans(x1, y1)``
        N)r   r   rZ   _old_values
_aux_transr
   rM   )r   	aux_transrN   rR   rS   rX   rY   r   r   r   r     s   

zGridHelperCurveLinear.__init__c                 K   s2   |d ur
| j | | j jdi | |   d S )Nr   )rM   update_transformupdate
invalidate)r   r   kwr   r   r   update_grid_finder4  s   z(GridHelperCurveLinear.update_grid_finderc                 C   sF   |   r| j||||fkrdS | |||| ||||f| _d| _dS )z!bbox in 0-based image coordinatesNF)validr   _update_grid_force_update)r   r[   r\   r]   r^   r   r   r   _update:  s
   
zGridHelperCurveLinear._updatec                 C   s:   |d u r| j }|d u r|}t| ||d}t|||d}|S )N)r   )rC   )r   r   r	   )r   r   r   rC   offsetr   _helperaxisliner   r   r   new_fixed_axisC  s   z$GridHelperCurveLinear.new_fixed_axisr-   c                 C   sF   |d u r| j }t| |||}t||}|jd |j|j j |S )NT)r   r;   r	   lineset_clip_onset_clip_boxbbox)r   r   r=   r   rC   r   r   r   r   r   new_floating_axisR  s   
	z'GridHelperCurveLinear.new_floating_axisc                 C   s   | j ||||| _d S r%   )rM   get_grid_inforZ   )r   r[   r]   r\   r^   r   r   r   r   n  s   z"GridHelperCurveLinear._update_gridmajorbothc                 C   sT   g }|dv r| j d d D ]}|| q|dv r(| j d d D ]}|| q |S )N)r   r   lonlines)r   r   lat)rZ   extend)r   whichaxis
grid_linesglr   r   r   get_gridlinesq  s   z#GridHelperCurveLinear.get_gridlinesFc           
      c   s    t ddddd| }ddg| }|s8t| j| d | | j| d | D ]\\}}}|}	||	||fV  q&d S t| j| d | | j| d | D ]\\}}}|}	||	|dfV  qKd S )	NZ   r   )r,   r+   r-   r.   r   r   	tick_locstick_labels )dictr   rZ   )
r   r   	axis_sider/   angle_tangent
lon_or_latxyar   angle_normalr   r   r   r0   {  s$   z'GridHelperCurveLinear.get_tick_iterator)NNNNNr%   )NNNN)Nr-   )r   r   )F)r6   r7   r8   r   r   r   r   r   r   r   r0   r:   r   r   r   r   r     s(    





r   )r9   	itertoolsr   numpyr>   matplotlib.pathr   matplotlib.transformsr   r   	axislinesr   r   axis_artistr	   rM   r
   r   Fixedr   Floatingr;   r   r   r   r   r   <module>   s    > K