o
    f]N                     @  s  d dl mZ d dlZd dlmZmZmZ d dlmZ	 d dl
mZ d dlZd dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZ d dlmZ erjd d	lmZ d d
lm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z' G dd deZ(G dd de(Z)G dd de(Z*dS )    )annotationsN)TYPE_CHECKINGAnycast)FillTypeLineType)convert_filledconvert_lines)as_fill_typeas_line_type)filled_to_mpl_pathslines_to_mpl_paths)Renderer)Sequence)Axes)Figure)	ArrayLikec                   @  s   e Zd ZU dZded< ded< ded< 					
		dXdYddZdZddZdZddZd[d!d"Z	#	$	%d\d]d.d/Z		#	0	1		#d^d_d7d8Z
	#	$	9	d`dad?d@Z	#	0dbdcdCdDZdddedHdIZdfdKdLZdZdMdNZdgdhdPdQZ	#	R	S	EdidjdVdWZdS )kMplRenderera  Utility renderer using Matplotlib to render a grid of plots over the same (x, y) range.

    Args:
        nrows (int, optional): Number of rows of plots, default ``1``.
        ncols (int, optional): Number of columns of plots, default ``1``.
        figsize (tuple(float, float), optional): Figure size in inches, default ``(9, 9)``.
        show_frame (bool, optional): Whether to show frame and axes ticks, default ``True``.
        backend (str, optional): Matplotlib backend to use or ``None`` for default backend.
            Default ``None``.
        gridspec_kw (dict, optional): Gridspec keyword arguments to pass to ``plt.subplots``,
            default None.
    zSequence[Axes]_axesr   _figbool_want_tight   	   r   TNnrowsintncolsfigsizetuple[float, float]
show_framebackend
str | Nonegridspec_kwdict[str, Any] | NonereturnNonec                 C  s   |d urdd l }|| |dddd}|d ur||d< nddi|d< tj||fi |\| _}	|	 | _|sB| jD ]}
|
d	 q:d| _d S )
Nr   FT)r   ZsqueezeZsharexZshareyr#   ZaspectZequalZ
subplot_kwZoff)	Z
matplotlibZusepltZsubplotsr   Zflattenr   Zaxisr   )selfr   r   r   r    r!   r#   ZmplkwargsZaxesax r+   ;lib/python3.10/site-packages/contourpy/util/mpl_renderer.py__init__+   s   	




zMplRenderer.__init__c                 C  s   t | drt| j d S d S )Nr   )hasattrr'   closer   r(   r+   r+   r,   __del__G   s   
zMplRenderer.__del__c                 C  sT   | j D ]}t|ddr|jdd d|_q| jr&t| j dkr(| j  d S d S d S )N_need_autoscaleFT)Ztightr   )r   getattrZautoscale_viewr2   r   lenr   Ztight_layoutr(   r*   r+   r+   r,   
_autoscaleK   s   
zMplRenderer._autoscaler*   
Axes | intr   c                 C  s   t |tr
| j| }|S N)
isinstancer   r   r5   r+   r+   r,   _get_axV   s   

zMplRenderer._get_axr   C0ffffff?filledcpy.FillReturn	fill_typeFillType | strcolorstralphafloatc                 C  sD   t |}| |}t||}tj||dd|d}|| d|_dS )a  Plot filled contours on a single Axes.

        Args:
            filled (sequence of arrays): Filled contour data as returned by
                :func:`~contourpy.ContourGenerator.filled`.
            fill_type (FillType or str): Type of ``filled`` data as returned by
                :attr:`~contourpy.ContourGenerator.fill_type`, or string equivalent
            ax (int or Maplotlib Axes, optional): Which axes to plot on, default ``0``.
            color (str, optional): Color to plot with. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        noner   Z
facecolorsZ
edgecolorslwrC   TN)r
   r:   r   mcollectionsPathCollectionadd_collectionr2   )r(   r=   r?   r*   rA   rC   paths
collectionr+   r+   r,   r=   [   s   




zMplRenderer.filledblack皙?xr   ypoint_colorquad_as_tri_alphac                 C  s   |  |}| ||\}}||d}|j|||j|jfi | |dkrd|ddddf |ddddf  |ddddf  |ddddf   }	d|ddddf |ddddf  |ddddf  |ddddf   }
||d< |jt|ddddf |	|ddddf fdt|ddddf |
|ddddf fdt|ddddf |	|ddddf fdt|ddddf |
|ddddf fdfi | |dur|j||||d	dd
 d|_dS )a  Plot quad grid lines on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color to plot grid lines, default ``"black"``.
            alpha (float, optional): Opacity to plot lines with, default ``0.1``.
            point_color (str, optional): Color to plot grid points or ``None`` if grid points
                should not be plotted, default ``None``.
            quad_as_tri_alpha (float, optional): Opacity to plot ``quad_as_tri`` grid, default 0.

        Colors may be a string color or the letter ``"C"`` followed by an integer in the range
        ``"C0"`` to ``"C9"`` to use a color from the ``tab10`` colormap.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        )rA   rC   r   g      ?Nr   rC   )   rS   o)rA   rC   ZmarkerrG   T)r:   _grid_as_2dplotTnpstackZreshaper2   )r(   rO   rP   r*   rA   rC   rQ   rR   r)   xmidymidr+   r+   r,   gridx   s&   

TT4444
zMplRenderer.grid      ?linescpy.LineReturn	line_typeLineType | str	linewidthc           	      C  sD   t |}| |}t||}tj|d|||d}|| d|_dS )aR  Plot contour lines on a single Axes.

        Args:
            lines (sequence of arrays): Contour line data as returned by
                :func:`~contourpy.ContourGenerator.lines`.
            line_type (LineType or str): Type of ``lines`` data as returned by
                :attr:`~contourpy.ContourGenerator.line_type`, or string equivalent.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color to plot lines. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.
        rE   rF   TN)r   r:   r   rH   rI   rJ   r2   )	r(   r_   ra   r*   rA   rC   rc   rK   rL   r+   r+   r,   r_      s   




zMplRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any]c                 C  sT   t j|}|t jju rdS | |}| ||\}}|j|| || d|d dS )a  Plot masked out grid points as circles on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (masked array of shape (ny, nx): z-values.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        NrU   )c)rY   ZmaZgetmaskZnomaskr:   rV   rW   )r(   rO   rP   rd   r*   rA   maskr+   r+   r,   rg      s   
zMplRenderer.maskFfilenametransparentc                 C  s   |    | jj||d dS )zSave plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.
        )ri   N)r6   r   savefig)r(   rh   ri   r+   r+   r,   save   s   zMplRenderer.save
io.BytesIOc                 C  s.   |    t }| jj|dd |d |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)formatr   )r6   ioBytesIOr   rj   seek)r(   bufr+   r+   r,   save_to_buffer   s
   
zMplRenderer.save_to_bufferc                 C  s   |    t  dS )zMShow plots in an interactive window, in the usual Matplotlib manner.
        N)r6   r'   showr0   r+   r+   r,   rs      s   zMplRenderer.showtitlec                 C  s0   |r|  |j||d dS |  || dS )a  Set the title of a single Axes.

        Args:
            title (str): Title text.
            ax (int or Matplotlib Axes, optional): Which Axes to set the title of, default ``0``.
            color (str, optional): Color to set title. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default is ``None`` which uses Matplotlib's default title color
                that depends on the stylesheet in use.
        )rA   N)r:   Z	set_title)r(   rt   r*   rA   r+   r+   r,   rt      s   zMplRenderer.titlegreen.1ffmtquad_as_tric                 C  s4  |  |}| ||\}}t|}|j\}}	t|D ]$}
t|	D ]}|j||
|f ||
|f ||
|f | dd|dd q!q|rt|d D ]O}
t|	d D ]D}t||
|
d ||d f }t||
|
d ||d f }t||
|
d ||d f }|j|||| dd|dd qPqHdS dS )a  Show ``z`` values on a single Axes.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (array-like of shape (ny, nx): z-values.
            ax (int or Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Color of added text. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``tab10`` colormap. Default ``"green"``.
            fmt (str, optional): Format to display z-values, default ``".1f"``.
            quad_as_tri (bool, optional): Whether to show z-values at the ``quad_as_tri`` centers
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        centerTZhaZvarA   Zclip_onr      N)r:   rV   rY   asarrayshaperangetextmean)r(   rO   rP   rd   r*   rA   rw   rx   nynxjiZxxZyyzzr+   r+   r,   z_values  s,   


,"""zMplRenderer.z_values)r   r   r   TNN)r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   r&   )r%   r&   )r*   r7   r%   r   )r   r;   r<   )r=   r>   r?   r@   r*   r7   rA   rB   rC   rD   r%   r&   )r   rM   rN   Nr   )rO   r   rP   r   r*   r7   rA   rB   rC   rD   rQ   r"   rR   rD   r%   r&   )r   r;   r^   r   )r_   r`   ra   rb   r*   r7   rA   rB   rC   rD   rc   rD   r%   r&   )r   rM   )rO   r   rP   r   rd   re   r*   r7   rA   rB   r%   r&   )F)rh   rB   ri   r   r%   r&   )r%   rl   )r   N)rt   rB   r*   r7   rA   r"   r%   r&   )r   ru   rv   F)rO   r   rP   r   rd   r   r*   r7   rA   rB   rw   rB   rx   r   r%   r&   )__name__
__module____qualname____doc____annotations__r-   r1   r6   r:   r=   r]   r_   rg   rk   rr   rs   rt   r   r+   r+   r+   r,   r      sT   
 


	!3$

r   c                      s*   e Zd ZdZ			dd fddZ  ZS )MplTestRendererzTest renderer implemented using Matplotlib.

    No whitespace around plots and no spines/ticks displayed.
    Uses Agg backend, so can only save to file/buffer, cannot call ``show()``.
    r   r   r   r   r   r   r   r%   r&   c                   sh   ddddddd}t  j|||dd|d | jD ]}|d |d |g  |g  qd| _d S )	Ng{Gz?gGz?)leftrighttopbottomZwspaceZhspaceTZAgg)r    r!   r#           F)superr-   r   Zset_xmarginZset_ymarginZ
set_xticksZ
set_yticksr   )r(   r   r   r   Zgridspecr*   	__class__r+   r,   r-   >  s    




zMplTestRenderer.__init__)r   r   r   )r   r   r   r   r   r   r%   r&   )r   r   r   r   r-   __classcell__r+   r+   r   r,   r   8  s    r   c                      s   e Zd ZdZ				dEdF fddZdGddZ							 	!dHdI fd+d,Z			-			 	!dJdK fd3d4Z		 dLdMd9d:Z		;dNdMd<d=Z		>		?dOdPdCdDZ
  ZS )QMplDebugRendererzDebug renderer implemented using Matplotlib.

    Extends ``MplRenderer`` to add extra information to help in debugging such as markers, arrows,
    text, etc.
    r   r   Tr   r   r   r   r   r    r   r%   r&   c                   s   t  |||| d S r8   )r   r-   )r(   r   r   r   r    r   r+   r,   r-   _  s   zMplDebugRenderer.__init__r*   r   
line_startcpy.CoordinateArrayline_endrA   rB   rC   rD   
arrow_sizec                 C  s   d||  }|| }|t t || }t |d |d  f}	t ||d |	 |  ||d |  ||d |	 |  f}
|j|
d d df |
d d df d||d d S )Ng      ?r   r   -rf   rC   )rY   Zsqrtdotr|   rZ   rW   )r(   r*   r   r   rA   rC   r   ZmidZalongr   Zarrowr+   r+   r,   _arrowh  s   	0zMplDebugRenderer._arrowr   C1r<   r;   redrN   r=   r>   r?   r@   r7   
line_color
line_alpharQ   start_point_colorc                   s  t |}t ||||| |d u r|d u rd S | |}t||tj}|d urt| D ]W\}}|d u r5q,t|d d |dd  D ]@\}}||| }|j|d d df |d d df ||d |
dkrt	|}t
|d D ]}| ||| ||d  |||
 qoqBq,|d urt| D ]d\}}|d u rqtj|d td}d||dd  d < |	d ur|d d }d||< |j|d d df | |d d df | d||d |	d ur|j|d d df | |d d df | d|	|d qd S d S )	NrS   r   r   r   r   )ZdtypeFrU   )r
   r   r=   r:   r   r   ZChunkCombinedOffsetziprW   r4   r~   r   rY   Zonesr   )r(   r=   r?   r*   rA   rC   r   r   rQ   r   r   ZpointsZoffsetsstartendZxysnr   rg   Zstart_indicesr   r+   r,   r=   |  sL   
"*"
**zMplDebugRenderer.filledr^   r_   r`   ra   rb   rc   c
              
     sT  t |}t |||||| |	dkr|d u rd S | |}t||tj}
tr-tt	j
|
}
|	dkrQ|
D ]}tt|d D ]}| ||| ||d  |||	 q=q3|d ur|
D ]P}d}t|}|d ur|j|d |d d||d d}|d d |d d kr|d d |d d kr|d8 }|j|||df |||df d||d qWd S d S )	Nr   r   r   )r   r   )r   r   rU   r   rS   )r   r   r_   r:   r	   r   ZSeparater   r   cpyZLineReturn_Separater~   r4   r   rW   )r(   r_   ra   r*   rA   rC   rc   rQ   r   r   Zseparate_linesliner   start_indexZ	end_indexr   r+   r,   r_     s6   
"0"zMplDebugRenderer.linesrO   r   rP   rd   c                 C  s   |  |}| ||\}}t|}|j\}}t|D ]%}t|D ]}	|	||  }
|j|||	f |||	f t|
dd|dd q!qd S )Nr   r   Trz   r:   rV   rY   r|   r}   r~   r   rB   )r(   rO   rP   rd   r*   rA   r   r   r   r   quadr+   r+   r,   point_numbers  s   


$zMplDebugRenderer.point_numbersbluec                 C  s   |  |}| ||\}}t|}|j\}}td|D ]F}td|D ]>}	|	||  }
||d |d |	d |	d f  }||d |d |	d |	d f  }|j||t|
dd|dd q#qd S )Nr   ry   Trz   )	r:   rV   rY   r|   r}   r~   r   r   rB   )r(   rO   rP   rd   r*   rA   r   r   r   r   r   r[   r\   r+   r+   r,   quad_numbers  s   


((zMplDebugRenderer.quad_numbersNru   lower_levelupper_levelfloat | Nonec                 C  s   |  |}| ||\}}t|}|j\}}	t|D ]9}
t|	D ]2}||
|f }|d ur4||kr4d}n	||kr;d}nd}|j||
|f ||
|f t|dd|dd q!qd S )Nr{   r   r   r   r   Trz   r   )r(   rO   rP   rd   r   r   r*   rA   r   r   r   r   r   Zz_levelr+   r+   r,   z_levels  s"   



$zMplDebugRenderer.z_levels)r   r   r   T)
r   r   r   r   r   r   r    r   r%   r&   )r*   r   r   r   r   r   rA   rB   rC   rD   r   rD   r%   r&   )r   r   r<   r;   r<   r;   r   rN   )r=   r>   r?   r@   r*   r7   rA   rB   rC   rD   r   rB   r   rD   rQ   rB   r   rB   r   rD   r%   r&   )r   r;   r^   r   r;   r   rN   )r_   r`   ra   rb   r*   r7   rA   rB   rC   rD   rc   rD   rQ   rB   r   rB   r   rD   r%   r&   )r   r   )rO   r   rP   r   rd   r   r*   r7   rA   rB   r%   r&   )r   r   )Nr   ru   )rO   r   rP   r   rd   r   r   rD   r   r   r*   r7   rA   rB   r%   r&   )r   r   r   r   r-   r   r=   r_   r   r   r   r   r+   r+   r   r,   r   Y  sF    
	9-r   )+Z
__future__r   rn   typingr   r   r   Zmatplotlib.collectionscollectionsrH   Zmatplotlib.pyplotZpyplotr'   ZnumpyrY   Z	contourpyr   r   Zcontourpy.convertr   r	   Zcontourpy.enum_utilr
   r   Zcontourpy.util.mpl_utilr   r   Zcontourpy.util.rendererr   Zcollections.abcr   Zmatplotlib.axesr   Zmatplotlib.figurer   Znumpy.typingr   Zcontourpy._contourpyZ
_contourpyr   r   r   r   r+   r+   r+   r,   <module>   s,       !