a
    |=icK                     @   s~   d dl 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mZ G dd dZG dd deZG d	d
 d
eZdS )    N)FillTypeLineType   )filled_to_mpl_pathslines_to_mpl_pathsmpl_codes_to_offsetsc                   @   s   e Zd ZdZd+ddZdd	 Zd
d Zdd Zdd Zd,ddZ	d-ddZ
d.ddZd/ddZd0dd Zd!d" Zd#d$ Zd1d%d&Zd2d)d*ZdS )3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.
    r   	   r
   TNc                 C   s   |d urdd l }|| t|dddd}|d ur<||d< ntdd|d< tj||fi |\| _}	|	 | _|s| jD ]}
|
d	 qxd| _	d S )
Nr   FT)figsizeZsqueezeZsharexZshareygridspec_kwequal)ZaspectZ
subplot_kwoff)

matplotlibZusedictpltZsubplots_figflatten_axesZaxis_want_tight)selfnrowsncolsr   
show_framebackendr   r   kwargsZaxesax r   l/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/contourpy/util/mpl_renderer.py__init__   s    



zMplRenderer.__init__c                 C   s   t | drt| j d S )Nr   )hasattrr   closer   r   r   r   r   __del__.   s    
zMplRenderer.__del__c                 C   sL   | j D ]"}t|ddr|jdd d|_q| jrHt| j dkrH| j  d S )N_need_autoscaleFT)Ztightr   )r   getattrZautoscale_viewr$   r   lenr   Ztight_layoutr   r   r   r   r   
_autoscale2   s    
zMplRenderer._autoscalec                 C   s   t |tr| j| }|S N)
isinstanceintr   r'   r   r   r   _get_ax=   s    

zMplRenderer._get_axc                 C   s6   t |}t |}|jdkr.t ||\}}||fS )Nr   )npasarrayndimZmeshgrid)r   xyr   r   r   _grid_as_2dB   s
    


zMplRenderer._grid_as_2dr   C0ffffff?c                 C   s<   |  |}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): Type of ``filled`` data, as returned by
                :attr:`~contourpy.ContourGenerator.fill_type`.
            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
edgecolorsZlwalphaTN)r,   r   mcollectionsPathCollectionadd_collectionr$   )r   filled	fill_typer   colorr7   paths
collectionr   r   r   r;   I   s    



zMplRenderer.filledblack皙?c                 C   s  |  |}| ||\}}t||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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.
        )r=   r7   r   g      ?Nr   r7   )   rB   o)r=   r7   markerT)
r,   r2   r   plotTr-   stackZreshapeZscatterr$   )r   r0   r1   r   r=   r7   point_colorZquad_as_tri_alphar   xmidymidr   r   r   grid^   s&    

TT4444
zMplRenderer.grid      ?c           	      C   s<   |  |}t||}tj|d|||d}|| d|_dS )a6  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): Type of ``lines`` data, as returned by
                :attr:`~contourpy.ContourGenerator.line_type`.
            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``.
        r5   r6   TN)r,   r   r8   r9   r:   r$   )	r   lines	line_typer   r=   r7   	linewidthr>   r?   r   r   r   rN      s    



zMplRenderer.linesc                 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"``.
        NrD   )c)r-   maZgetmaskZnomaskr,   r2   rF   )r   r0   r1   zr   r=   maskr   r   r   rT      s    

zMplRenderer.maskFc                 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``.
        )transparentN)r(   r   savefig)r   filenamerU   r   r   r   save   s    zMplRenderer.savec                 C   s.   |    t }| jj|dd |d |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)formatr   )r(   ioBytesIOr   rV   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)r(   r   showr"   r   r   r   r_      s    zMplRenderer.showc                 C   s.   |r|  |j||d n|  || 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 ``None`` which is ``black``.
        )r=   N)r,   	set_title)r   titler   r=   r   r   r   ra      s    
zMplRenderer.titlegreen.1fc                 C   s2  |  |}| ||\}}t|}|j\}}	t|D ]H}
t|	D ]:}|j||
|f ||
|f ||
|f | dd|dd qBq6|r.t|d D ]}
t|	d D ]}t||
|
d ||d f }t||
|
d ||d f }t||
|
d ||d f }|j|||| dd|dd qqd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havar=   Zclip_onr      N)r,   r2   r-   r.   shaperangetextmean)r   r0   r1   rS   r   r=   fmtZquad_as_trinynxjixxyyzzr   r   r   z_values   s$    


,
"""zMplRenderer.z_values)r   r   r	   TNN)r   r3   r4   )r   r@   rA   Nr   )r   r3   rM   r   )r   r@   )F)r   N)r   rb   rc   F)__name__
__module____qualname____doc__r   r#   r(   r,   r2   r;   rL   rN   rT   rX   r^   r_   ra   rt   r   r   r   r   r      s     


&



r   c                       s"   e Zd ZdZ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	   c                    sh   ddddddd}t  j|||dd|d | jD ],}|d |d |g  |g  q0d| _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	   )ru   rv   rw   rx   r   __classcell__r   r   r   r   ry      s   ry   c                       sd   e Zd ZdZd fdd	Zdd Zd fdd	Zd fdd	Zd ddZd!ddZ	d"ddZ
  ZS )#MplDebugRendererzDebug renderer implemented using Matplotlib.

    Extends ``MplRenderer`` to add extra information to help in debugging such as markers, arrows,
    text, etc.
    r   r	   Tc                    s   t  |||| d S r)   )r   r   )r   r   r   r   r   r   r   r   r     s    zMplDebugRenderer.__init__c                 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   -rQ   r7   )r-   sqrtdotr.   rH   rF   )r   r   
line_startline_endr=   r7   
arrow_sizemidZalongr{   arrowr   r   r   _arrow   s    zMplDebugRenderer._arrowr   C1r4   r3   redrA   c                    s  t  ||||| |d u r(|d u r(d S | |}|tjkrZ|d }dd |d D }n|tjkrdd |d D }dd |d D }nn|tjkr|d }|d }nP|tjkrdd |d D }dd |d D }n|tjkrPg }g }t	| D ]T\}}}|d u rq|t
||dd	 7 }t
||dd	 }|d
d |D 7 }qn|tjkrg }g }t	| D ]z\}}}|d u rqltt|d D ]N}||| ||d  d  }|||d |d	   |||d   qqlntd| d|d urt	||D ]\}}t	|d d	 |dd  D ]\}}||| }|j|d d df |d d df ||d |
dkr0t|}t|d D ]&}| ||| ||d  |||
 qq0q|d urt	||D ]\}}t
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 )Nr   c                 S   s   g | ]}t |qS r   r   .0codesr   r   r   
<listcomp>7      z+MplDebugRenderer.filled.<locals>.<listcomp>r   c                 S   s   g | ]}|d ur|qS r)   r   r   pointsr   r   r   r   9  r   c                 S   s   g | ]}|d urt |qS r)   r   r   r   r   r   r   :  r   c                 S   s   g | ]}|d ur|qS r)   r   r   r   r   r   r   ?  r   c                 S   s   g | ]}|d ur|qS r)   r   )r   offsetsr   r   r   r   @  r   rB   c                 S   s   g | ]}t |qS r   r   r   r   r   r   r   I  r   zRendering FillType  not implementedr   r~   )ZdtypeFrD   )r   r;   r,   r   Z	OuterCodeChunkCombinedCodeZOuterOffsetChunkCombinedOffsetZChunkCombinedCodeOffsetzipr-   splitZChunkCombinedOffsetOffsetri   r&   appendRuntimeErrorrF   r   Zonesbool)r   r;   r<   r   r=   r7   Z
line_colorZ
line_alpharI   start_point_colorr   Z
all_pointsZall_offsetsr   r   Zouter_offsetsr   rp   ZoffsstartendZxysnrT   Zstart_indicesr   r   r   r;   ,  sv    







"*
,

*
*zMplDebugRenderer.filledrM   c
              
      s4  t  |||||| |	dkr*|d u r*d S | |}|tjkrD|}
n|tjkrX|d }
n|tjkrg }
t| D ]L\}}|d urnt|}t	t
|d D ]"}|
||| ||d    qqnnp|tjkrg }
t| D ]D\}}|d urt	t
|d D ]"}|
||| ||d    qqntd| d|	dkr||
D ]>}t	t
|d D ]&}| ||| ||d  |||	 qPq<|d ur0|
D ]}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	 qd S )Nr~   r   r   zRendering LineType r   )r   r   )r   r   rD   r   rB   )r   rN   r,   r   ZSeparateZSeparateCoder   r   r   ri   r&   r   r   r   r   rF   )r   rN   rO   r   r=   r7   rP   rI   r   r   	all_linesr   r   r   rp   linestart_indexZ	end_indexr   r   r   rN   r  sL    




$$
(

4"zMplDebugRenderer.linesc                 C   s   |  |}| ||\}}t|}|j\}}t|D ]J}t|D ]<}	|	||  }
|j|||	f |||	f t|
dd|dd qBq6d S )Nr{   r|   Tre   )r,   r2   r-   r.   rh   ri   rj   str)r   r0   r1   rS   r   r=   rm   rn   ro   rp   quadr   r   r   point_numbers  s    


$zMplDebugRenderer.point_numbersbluec                 C   s   |  |}| ||\}}t|}|j\}}td|D ]}td|D ]|}	|	||  }
||d |d |	d |	d f  }||d |d |	d |	d f  }|j||t|
dd|dd qFq8d S )Nr   rd   Tre   )	r,   r2   r-   r.   rh   ri   rk   rj   r   )r   r0   r1   rS   r   r=   rm   rn   ro   rp   r   rJ   rK   r   r   r   quad_numbers  s    


((zMplDebugRenderer.quad_numbersNrb   c                 C   s   |  |}| ||\}}t|}|j\}}	t|D ]n}
t|	D ]`}||
|f }|d urh||krhd}n||krvd}nd}|j||
|f ||
|f |dd|dd qBq6d S )Nrg   r   r   rz   r}   Tre   )r,   r2   r-   r.   rh   ri   rj   )r   r0   r1   rS   Zlower_levelZupper_levelr   r=   rm   rn   ro   rp   rs   Zz_levelr   r   r   z_levels  s    


 zMplDebugRenderer.z_levels)r   r   r	   T)r   r   r4   r3   r4   r3   r   rA   )r   r3   rM   r   r3   r   rA   )r   r   )r   r   )Nr   rb   )ru   rv   rw   rx   r   r   r;   rN   r   r   r   r   r   r   r   r   r     s     F  .

r   )rZ   Zmatplotlib.collectionscollectionsr8   Zmatplotlib.pyplotZpyplotr   numpyr-   Z	contourpyr   r   Zmpl_utilr   r   r   r   ry   r   r   r   r   r   <module>   s    p