
    YcK                         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          Z G d de          Z G d	 d
e          ZdS )    N)FillTypeLineType   )filled_to_mpl_pathslines_to_mpl_pathsmpl_codes_to_offsetsc                   x    e Zd ZdZ	 ddZd Zd Zd	 Zd
 ZddZ	ddZ
d dZd!dZd"dZd Zd Zd#dZd$dZdS )%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                 P   |dd l }|                    |           t          |ddd          }|||d<   nt          d          |d<   t          j        ||fi |\  | _        }	|	                                | _        |s| j        D ]}
|
                    d	           d| _	        d S )
Nr   FT)figsizesqueezesharexshareygridspec_kwequal)aspect
subplot_kwoff)

matplotlibusedictpltsubplots_figflatten_axesaxis_want_tight)selfnrowsncolsr   
show_framebackendr   r   kwargsaxesaxs              ;lib/python3.11/site-packages/contourpy/util/mpl_renderer.py__init__zMplRenderer.__init__   s      	$NN7###guT$OOO 	8$/F=!!#'w#7#7#7F< ,ue>>v>>	4\\^^
 	j      c                 \    t          | d          rt          j        | j                   d S d S )Nr   )hasattrr   closer   r!   s    r)   __del__zMplRenderer.__del__.   s6    4   	!Idi     	! 	!r+   c                     | j         D ]0}t          |dd          r|                    d           d|_        1| j        r3t          | j                   dk    r| j                                         d S d S d S )N_need_autoscaleFT)tightr   )r   getattrautoscale_viewr2   r    lenr   tight_layoutr!   r(   s     r)   
_autoscalezMplRenderer._autoscale2   s     * 	+ 	+Br,e44 +!!!---%*" 	%DJ! 3 	%I""$$$$$	% 	% 	% 	%r+   c                 J    t          |t                    r| j        |         }|S N)
isinstanceintr   r8   s     r)   _get_axzMplRenderer._get_ax=   s$    b# 	 BB	r+   c                     t          j        |          }t          j        |          }|j        dk    rt          j        ||          \  }}||fS )Nr   )npasarrayndimmeshgrid)r!   xys      r)   _grid_as_2dzMplRenderer._grid_as_2dB   sH    JqMMJqMM6Q; 	%;q!$$DAq!tr+   r   C0ffffff?c                     |                      |          }t          ||          }t          j        ||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   
facecolors
edgecolorslwalphaTN)r>   r   mcollectionsPathCollectionadd_collectionr2   )r!   filled	fill_typer(   colorrO   paths
collections           r)   rS   zMplRenderer.filledI   sk     \\"#FI66!0e1EK K K

*%%%!r+   black皙?c                 2   |                      |          }|                     ||          \  }}t          ||          } |j        |||j        |j        fi | |dk    rd|ddddf         |ddddf         z   |ddddf         z   |ddddf         z   z  }	d|ddddf         |ddddf         z   |ddddf         z   |ddddf         z   z  }
||d<    |j        t          j        |ddddf         |	|ddddf         f                              d          t          j        |ddddf         |
|ddddf         f                              d          t          j        |ddddf         |	|ddddf         f                              d          t          j        |ddddf         |
|ddddf         f                              d          fi | ||                    ||||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.
        )rU   rO   r   g      ?Nr   rO   )   r[   o)rU   rO   markerT)
r>   rF   r   plotTr@   stackreshapescatterr2   )r!   rD   rE   r(   rU   rO   point_colorquad_as_tri_alphar&   xmidymids              r)   gridzMplRenderer.grid^   s   & \\"1%%1E///1ac13))&)))q  
	3B38qSbSz1Acrc122gJ>122qrr6JKD3B38qSbSz1Acrc122gJ>122qrr6JKD/F7OBG!CRC"H+tQqrr122vY788@@II!CRC"H+tQqrr122vY788@@II!ABBG*dAcrc122gJ788@@II!ABBG*dAcrc122gJ788@@II	 
     	IJJq!;eCJHHH!r+         ?c                     |                      |          }t          ||          }t          j        |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``.
        rJ   rK   TN)r>   r   rP   rQ   rR   r2   )	r!   lines	line_typer(   rU   rO   	linewidthrV   rW   s	            r)   rk   zMplRenderer.lines   sk     \\""5)44!0f9ES S S

*%%%!r+   c                    t           j                            |          }|t           j        j        u rdS |                     |          }|                     ||          \  }}|                    ||         ||         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"``.
        Nr]   )c)r@   magetmasknomaskr>   rF   r_   )r!   rD   rE   zr(   rU   masks          r)   rt   zMplRenderer.mask   s     u}}Q25< 	F\\"1%%1
$4#/////r+   Fc                 f    |                                   | j                            ||           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)r9   r   savefig)r!   filenamerv   s      r)   savezMplRenderer.save   s5     		(<<<<<r+   c                     |                                   t          j                    }| j                            |d           |                    d           |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        png)formatr   )r9   ioBytesIOr   rw   seek)r!   bufs     r)   save_to_bufferzMplRenderer.save_to_buffer   sN     	jll	#e,,,
r+   c                 T    |                                   t          j                     dS )zMShow plots in an interactive window, in the usual Matplotlib manner.
        N)r9   r   showr/   s    r)   r   zMplRenderer.show   s#     	




r+   c                     |r,|                      |                              ||           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 ``None`` which is ``black``.
        )rU   N)r>   	set_title)r!   titler(   rU   s       r)   r   zMplRenderer.title   s]      	.LL&&uE&:::::LL&&u-----r+   green.1fc                    |                      |          }|                     ||          \  }}t          j        |          }|j        \  }}	t          |          D ]K}
t          |	          D ]9}|                    ||
|f         ||
|f         ||
|f         | dd|d           :L|rt          |dz
            D ]}
t          |	dz
            D ]}t          j        ||
|
dz   ||dz   f                   }t          j        ||
|
dz   ||dz   f                   }t          j        ||
|
dz   ||dz   f                   }|                    |||| dd|d           d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varU   clip_onr      N)r>   rF   r@   rA   shaperangetextmean)r!   rD   rE   rs   r(   rU   fmtquad_as_trinynxjixxyyzzs                  r)   z_valueszMplRenderer.z_values   s   $ \\"1%%1JqMMBr 	3 	3A2YY 3 3!Q$1a4Qq!tWc*<*<h#T  3 3 3 33  	*2a4[[ * *r!t * *A1QqS5!AaC%<11B1QqS5!AaC%<11B1QqS5!AaC%<11BGGBr3MMh8SX$(  * * * *	*	* 	** *r+   )r   r   r   TNN)r   rG   rH   )r   rX   rY   Nr   )r   rG   ri   r   )r   rX   )F)r   N)r   r   r   F)__name__
__module____qualname____doc__r*   r0   r9   r>   rF   rS   rh   rk   rt   ry   r   r   r   r    r+   r)   r
   r
      s          \`       *! ! !	% 	% 	%  
  " " " "*$" $" $" $"L" " " ",0 0 0 0"	= 	= 	= 	=
 
 
  . . . .!* !* !* !* !* !*r+   r
   c                   $     e Zd ZdZd fd	Z x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                 2   ddddddd}t                                          |||dd|           | j        D ]V}|                    d           |                    d           |                    g            |                    g            Wd| _        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    )r!   r"   r#   r   gridspecr(   	__class__s         r)   r*   zMplTestRenderer.__init__  s    
 
 	5'dEx 	 	
 	
 	
 * 	 	BNN3NN3MM"MM" r+   )r   r   r   )r   r   r   r   r*   __classcell__r   s   @r)   r   r      sG         
! ! ! ! ! ! ! ! ! !r+   r   c                   b     e Zd ZdZd fd	Zd Z	 	 d fd	Z	 	 d fd	ZddZddZ	ddZ
 x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                 P    t                                          ||||           d S r;   )r   r*   )r!   r"   r#   r   r$   r   s        r)   r*   zMplDebugRenderer.__init__  s'    w
;;;;;r+   c                    d||z   z  }||z
  }|t          j        t          j        ||                    z  }t          j        |d         |d          f          }	t          j        ||dz  |	z
  |z  z
  ||dz  |z  z   ||dz  |	z   |z  z
  f          }
|                    |
d d df         |
d d df         d||           d S )Ng      ?r   r   -ro   rO   )r@   sqrtdotrA   ra   r_   )r!   r(   
line_startline_endrU   rO   
arrow_sizemidalongr   arrows              r)   _arrowzMplDebugRenderer._arrow   s    :():%u--...
E!HuQxi01159u$j00%)J&&59u$j00
  
 	aaadU111a4[#eDDDDDr+   r   C1rH   rG   redrY   c                    t                                          |||||           ||d S |                     |          }|t          j        k    r|d         }d |d         D             }n|t          j        k    r&d |d         D             }d |d         D             }n|t          j        k    r|d         }|d         }nj|t          j        k    r&d |d         D             }d |d         D             }n4|t          j        k    rdg }g }t          | D ]U\  }}}|	|t          j        ||dd                   z  }t          j        ||dd                   }|d	 |D             z  }Vn|t          j        k    rg }g }t          | D ]\  }}}|	t          t          |          dz
            D ]e}|||         ||dz            dz            }|                    ||d         |d                             |                    ||d         z
             fnt!          d
| d          |t          ||          D ]\  }}t          |d d         |dd                    D ]\  }}|||         }|                    |d d df         |d d df         ||           |
dk    rMt          |          }t          |dz
            D ]+}|                     |||         ||dz            |||
           ,|t          ||          D ]\  }}t          j        |d         t(                    }d||dd          dz
  <   |	|d d         }d||<   |                    |d d df         |         |d d df         |         d||           |	:|                    |d d df         |         |d d df         |         d|	|           d S d S )Nr   c                 ,    g | ]}t          |          S r   r   .0codess     r)   
<listcomp>z+MplDebugRenderer.filled.<locals>.<listcomp>7  s!    NNN5/66NNNr+   r   c                     g | ]}||S r;   r   r   pointss     r)   r   z+MplDebugRenderer.filled.<locals>.<listcomp>9      OOOVFO&OOOr+   c                 0    g | ]}|t          |          S r;   r   r   s     r)   r   z+MplDebugRenderer.filled.<locals>.<listcomp>:  s'    ccc5QVc/66cccr+   c                     g | ]}||S r;   r   r   s     r)   r   z+MplDebugRenderer.filled.<locals>.<listcomp>?  r   r+   c                     g | ]}||S r;   r   )r   offsetss     r)   r   z+MplDebugRenderer.filled.<locals>.<listcomp>@  s    SSSwwS7SSSr+   r[   c                 ,    g | ]}t          |          S r   r   r   s     r)   r   z+MplDebugRenderer.filled.<locals>.<listcomp>I  s!    OOO 4U ; ;OOOr+   zRendering FillType  not implementedr   r   )dtypeFr]   )r   rS   r>   r   	OuterCodeChunkCombinedCodeOuterOffsetChunkCombinedOffsetChunkCombinedCodeOffsetzipr@   splitChunkCombinedOffsetOffsetr   r6   appendRuntimeErrorr_   r   onesbool)r!   rS   rT   r(   rU   rO   
line_color
line_alphard   start_point_colorr   
all_pointsall_offsetsr   r   outer_offsetsr   r   offsstartendxysnrt   start_indicesr   s                            r)   rS   zMplDebugRenderer.filled,  s   vy"eU;;; 	+ 	F\\"**  	RJNNF1INNNKK(44 	ROOvayOOOJccF1IcccKK(.. 	RJ )KK(66 	ROOvayOOOJSS&)SSSKK(:: 	RJK03V P P,} bhv}QrT/BCCC
ad(;<<OOOOOOP (<< 	RJK25v, 7 7. s=11!344 7 7A"=#3M!A#4Fq4H#HID%%fT!WT"X-=&>???&&td1g~666677 PYPPPQQQ  		b#&z;#?#? b b"%gcrclGABBK"@"@ b bJE3 s+CGGC1Is111a4yJjGQQQ!C' bHH!&qs b bA KKCFC!Hj*V`aaaab  	C#&z;#?#? C Cwwr{$777&+WQRR[]#$ 0$+CRCLM*/D'111a4L&qqq!tT(:C;V`  b b b % CGGF111a4L71m9TVY/z  C C C	C 	CC Cr+   ri   c
           
         t                                          ||||||           |	dk    r|d S |                     |          }|t          j        k    r|}
n |t          j        k    r
|d         }
n|t          j        k    rqg }
t          | D ]d\  }}|]t          |          }t          t          |          dz
            D ].}|
                    |||         ||dz                                /en|t          j        k    rbg }
t          | D ]U\  }}|Nt          t          |          dz
            D ].}|
                    |||         ||dz                                /Vnt          d| d          |	dk    rP|
D ]M}t          t          |          dz
            D ]+}|                     |||         ||dz            |||	           ,N||
D ]}d}t          |          }|i|                    |d         |d         d||	           d}|d         d         |d
         d         k    r#|d         d         |d
         d         k    r|dz  }|                    |||df         |||df         d||	           d S d S )Nr   r   r   zRendering LineType r   )r   r   )r   r   r]   r   r[   )r   rk   r>   r   SeparateSeparateCoder   r   r   r   r6   r   r   r   r   r_   )r!   rk   rl   r(   rU   rO   rm   rd   r   r   	all_linesr   r   r   r   linestart_index	end_indexr   s                     r)   rk   zMplDebugRenderer.linesr  s:   eYE5)DDD 	 	F\\")) 	RII(// 	RaII(44 	RI!$e J J J2599G"3w<<>22 J J!((
71Q3<0G)HIIII	J
 (66 	RI#&; J J J"3w<<>22 J J!((
71Q3<0G)HIIIIJ
 PYPPPQQQ 	R! R Rs4yy{++ R RAKKDGT!A#YujQQQQR  
	.! 	. 	.II	$ 'GGDJT
C;LTYGZZZ"#KAwqzT"Xa[0 'T!WQZ48A;5N '!Q	[2A56[=RTU=U8VX[u  . . . .
	. 
	.	. 	.r+   c                 h   |                      |          }|                     ||          \  }}t          j        |          }|j        \  }}t          |          D ]U}t          |          D ]C}	|	||z  z   }
|                    |||	f         |||	f         t          |
          dd|d           DVd S )Nr   r   Tr   )r>   rF   r@   rA   r   r   r   str)r!   rD   rE   rs   r(   rU   r   r   r   r   quads              r)   point_numberszMplDebugRenderer.point_numbers  s    \\"1%%1JqMMBr 	& 	&A2YY & &1R4x!Q$1a4#d))EQV $  & & & &&	& 	&r+   bluec                    |                      |          }|                     ||          \  }}t          j        |          }|j        \  }}t          d|          D ]}t          d|          D ]}	|	||z  z   }
||dz
  |dz   |	dz
  |	dz   f                                         }||dz
  |dz   |	dz
  |	dz   f                                         }|                    ||t          |
          dd|d           d S )Nr   r   Tr   )	r>   rF   r@   rA   r   r   r   r   r   )r!   rD   rE   rs   r(   rU   r   r   r   r   r   rf   rg   s                r)   quad_numberszMplDebugRenderer.quad_numbers  s%   \\"1%%1JqMMBq" 	d 	dA1b\\ d d1R4x1QqS!A#ac')*//111QqS!A#ac')*//11dCII(xu^bcccc	d	d 	dr+   Nr   c                 ~   |                      |          }|                     ||          \  }}t          j        |          }|j        \  }}	t          |          D ]`}
t          |	          D ]N}||
|f         }|	||k    rd}n||k    rd}nd}|                    ||
|f         ||
|f         |dd|d           Oad S )Nr   r   r   r   r   Tr   )r>   rF   r@   rA   r   r   r   )r!   rD   rE   rs   lower_levelupper_levelr(   rU   r   r   r   r   r   z_levels                 r)   z_levelszMplDebugRenderer.z_levels  s    \\"1%%1JqMMBr 
	& 
	&A2YY 	& 	&q!tW  rK/?  GG+%  GGG!Q$1a4'fQV $  & & & &	&
	& 
	&r+   )r   r   r   T)r   r   rH   rG   rH   rG   r   rY   )r   rG   ri   r   rG   r   rY   )r   r   )r   r   )Nr   r   )r   r   r   r   r*   r   rS   rk   r   r   r  r   r   s   @r)   r   r     s         
< < < < < <
E 
E 
E QUUXDC DC DC DC DC DCL ]a25,. ,. ,. ,. ,. ,.\	& 	& 	& 	&
d 
d 
d 
d& & & & & & & &r+   r   )r}   matplotlib.collectionscollectionsrP   matplotlib.pyplotpyplotr   numpyr@   	contourpyr   r   mpl_utilr   r   r   r
   r   r   r   r+   r)   <module>r     s   				 - - - - - -           ( ( ( ( ( ( ( ( S S S S S S S S S Sl* l* l* l* l* l* l* l*^! ! ! ! !k ! ! !8o& o& o& o& o&{ o& o& o& o& o&r+   