o
    Xεi]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   squeezesharexshareyr#   aspectequal
subplot_kwoff)	
matplotlibusepltsubplotsr   flattenr   axisr   )selfr   r   r   r    r!   r#   mplkwargsaxesax r9   O/home/ubuntu/.local/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   )hasattrr0   closer   r4   r9   r9   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)tightr   )r   getattrautoscale_viewr@   r   lenr   tight_layoutr4   r8   r9   r9   r:   
_autoscaleK   s   
zMplRenderer._autoscaler8   
Axes | intr   c                 C  s   t |tr
| j| }|S N)
isinstancer   r   rF   r9   r9   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   
facecolors
edgecolorslwrT   TN)r
   rK   r   mcollectionsPathCollectionadd_collectionr@   )r4   rN   rP   r8   rR   rT   paths
collectionr9   r9   r:   rN   [   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.
        )rR   rT   r   g      ?Nr   rT   )   rf   o)rR   rT   markerrZ   T)rK   _grid_as_2dplotTnpstackreshaper@   )r4   rb   rc   r8   rR   rT   rd   re   r6   xmidymidr9   r9   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``.
        rV   rW   TN)r   rK   r   r[   r\   r]   r@   )	r4   rt   rv   r8   rR   rT   rx   r^   r_   r9   r9   r:   rt      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"``.
        Nrh   )c)rm   magetmasknomaskrK   rj   rk   )r4   rb   rc   ry   r8   rR   maskr9   r9   r:   r      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``.
        )r   N)rG   r   savefig)r4   r   r   r9   r9   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.
        png)formatr   )rG   ioBytesIOr   r   seek)r4   bufr9   r9   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)rG   r0   showr>   r9   r9   r:   r      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.
        )rR   N)rK   	set_title)r4   r   r8   rR   r9   r9   r:   r      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havarR   clip_onr      N)rK   rj   rm   asarrayshaperangetextmean)r4   rb   rc   ry   r8   rR   r   r   nynxjixxyyzzr9   r9   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&   )r8   rH   r%   r   )r   rL   rM   )rN   rO   rP   rQ   r8   rH   rR   rS   rT   rU   r%   r&   )r   r`   ra   Nr   )rb   r   rc   r   r8   rH   rR   rS   rT   rU   rd   r"   re   rU   r%   r&   )r   rL   rs   r   )rt   ru   rv   rw   r8   rH   rR   rS   rT   rU   rx   rU   r%   r&   )r   r`   )rb   r   rc   r   ry   rz   r8   rH   rR   rS   r%   r&   )F)r   rS   r   r   r%   r&   )r%   r   )r   N)r   rS   r8   rH   rR   r"   r%   r&   )r   r   r   F)rb   r   rc   r   ry   r   r8   rH   rR   rS   r   rS   r   r   r%   r&   )__name__
__module____qualname____doc____annotations__r;   r?   rG   rK   rN   rr   rt   r   r   r   r   r   r   r9   r9   r9   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bottomwspacehspaceTAgg)r    r!   r#           F)superr;   r   set_xmarginset_ymargin
set_xticks
set_yticksr   )r4   r   r   r   gridspecr8   	__class__r9   r:   r;   >  s    




zMplTestRenderer.__init__)r   r   r   )r   r   r   r   r   r   r%   r&   )r   r   r   r   r;   __classcell__r9   r9   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 rI   )r   r;   )r4   r   r   r   r    r   r9   r:   r;   _  s   zMplDebugRenderer.__init__r8   r   
line_startcpy.CoordinateArrayline_endrR   rS   rT   rU   
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   -r{   rT   )rm   sqrtdotr   rn   rk   )r4   r8   r   r   rR   rT   r   midalongr   arrowr9   r9   r:   _arrowh  s   	0zMplDebugRenderer._arrowr   C1rM   rL   redra   rN   rO   rP   rQ   rH   
line_color
line_alphard   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 )	Nrf   r   r   r   r   )dtypeFrh   )r
   r   rN   rK   r   r   ChunkCombinedOffsetziprk   rD   r   r   rm   onesr   )r4   rN   rP   r8   rR   rT   r   r   rd   r   r   pointsoffsetsstartendxysnr   r   start_indicesr   r9   r:   rN   |  sL   
"*"
**zMplDebugRenderer.filledrs   rt   ru   rv   rw   rx   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   rh   r   rf   )r   r   rt   rK   r	   r   Separater   r   cpyLineReturn_Separater   rD   r   rk   )r4   rt   rv   r8   rR   rT   rx   rd   r   r   separate_linesliner   start_index	end_indexr   r9   r:   rt     s6   
"0"zMplDebugRenderer.linesrb   r   rc   ry   c                 C  s   |  |}| ||\}}t|}|j\}}t|D ]%}t|D ]}	|	||  }
|j|||	f |||	f t|
dd|dd q!qd S )Nr   r   Tr   rK   rj   rm   r   r   r   r   rS   )r4   rb   rc   ry   r8   rR   r   r   r   r   quadr9   r9   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   r   Tr   )	rK   rj   rm   r   r   r   r   r   rS   )r4   rb   rc   ry   r8   rR   r   r   r   r   r   rp   rq   r9   r9   r:   quad_numbers  s   


((zMplDebugRenderer.quad_numbersNr   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   Tr   r   )r4   rb   rc   ry   r   r   r8   rR   r   r   r   r   r   z_levelr9   r9   r:   z_levels  s"   



$zMplDebugRenderer.z_levels)r   r   r   T)
r   r   r   r   r   r   r    r   r%   r&   )r8   r   r   r   r   r   rR   rS   rT   rU   r   rU   r%   r&   )r   r   rM   rL   rM   rL   r   ra   )rN   rO   rP   rQ   r8   rH   rR   rS   rT   rU   r   rS   r   rU   rd   rS   r   rS   r   rU   r%   r&   )r   rL   rs   r   rL   r   ra   )rt   ru   rv   rw   r8   rH   rR   rS   rT   rU   rx   rU   rd   rS   r   rS   r   rU   r%   r&   )r   r   )rb   r   rc   r   ry   r   r8   rH   rR   rS   r%   r&   )r   r   )Nr   r   )rb   r   rc   r   ry   r   r   rU   r   r   r8   rH   rR   rS   r%   r&   )r   r   r   r   r;   r   rN   rt   r   r   r   r   r9   r9   r   r:   r   Y  sF    
	9-r   )+
__future__r   r   typingr   r   r   matplotlib.collectionscollectionsr[   matplotlib.pyplotpyplotr0   numpyrm   	contourpyr   r   contourpy.convertr   r	   contourpy.enum_utilr
   r   contourpy.util.mpl_utilr   r   contourpy.util.rendererr   collections.abcr   matplotlib.axesr   matplotlib.figurer   numpy.typingr   contourpy._contourpy
_contourpyr   r   r   r   r9   r9   r9   r:   <module>   s,       !