
    Yc8-                         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	m
Z
 d dlmZ d dlmZ d dlZdd	lmZmZ  G d
 d          ZdS )    N)
export_png
export_svgshow)get_screenshot_as_png)gridplot)Label)
Category10)figure   )filled_to_bokehlines_to_bokehc                   p    e Zd ZdZd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 )$BokehRendereraF  Utility renderer using Bokeh 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 (assuming 100 dpi), default
            ``(9, 9)``.
        show_frame (bool, optional): Whether to show frame and axes ticks, default ``True``.
        want_svg (bool, optional): Whether output is required in SVG format or not, default
            ``False``.

    Warning:
        :class:`~contourpy.util.bokeh_renderer.BokehRenderer`, unlike
        :class:`~contourpy.util.mpl_renderer.MplRenderer`, needs to be told in advance if output to
        SVG format will be required later, otherwise it will assume PNG output.
    r   	   r   TFc                    || _         t          d         | _        dt          j        |          z  }||z  }g | _        | j         rdnd}t          |          D ]Y}	t          |          }
d|
j        _	        d|
j
        _	        | j                            |
           |sd |
_        d|
j        _	        Zt          | j        |d |d         |z  |d         |z  	          | _        d S )
N
   d   svgcanvas)output_backendFr   r   )ncolstoolbar_locationwidthheight)	_want_svgr	   _palettenpasarray_figuresranger
   xgridvisibleygridappendoutline_line_coloraxisr   _layout)selfnrowsr   figsize
show_framewant_svg
total_sizenfiguresbackend_figs              =lib/python3.11/site-packages/contourpy/util/bokeh_renderer.py__init__zBokehRenderer.__init__   s    !"2G,,,
;>7%%xx 	) 	)A000C %CI %CIM  %%% ))-&#( MQ-5(A%1GI I I    c                     t          |t                    r0|d         dk    r$t          |dd                    }| j        |         }|S )Nr   Cr   )
isinstancestrintr   )r)   colorindexs      r3   _convert_colorzBokehRenderer._convert_color5   sG    eS!! 	)eAh#o 	)abb	NNEM%(Er5   c                 J    t          |t                    r| j        |         }|S )N)r8   r:   r    )r)   axs     r3   _get_figurezBokehRenderer._get_figure;   s%    b# 	#r"B	r5   c                     t          j        |          }t          j        |          }|j        dk    rt          j        ||          \  }}||fS )Nr   )r   r   ndimmeshgrid)r)   xys      r3   _grid_as_2dzBokehRenderer._grid_as_2d@   sH    JqMMJqMM6Q; 	%;q!$$DAq!tr5   r   C0ffffff?c                     |                      |          }|                     |          }t          ||          \  }}t          |          dk    r|                    |g|g||d           dS dS )a  Plot filled contours on a single plot.

        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 Bokeh Figure, optional): Which plot to use, 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
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        r   )xsysr;   
fill_alpha
line_widthN)r@   r=   r   lenmulti_polygons)	r)   filled	fill_typer?   r;   alphar2   rJ   rK   s	            r3   rP   zBokehRenderer.filledG   s     r""##E** 33Br77Q; 	^2$B4u[\]]]]]	^ 	^r5   black皙?Nc                    |                      |          }|                     ||          \  }}d |D             d |j        D             z   }	d |D             d |j        D             z   }
t          ||          } |j        |	|
fi | |dk    r2d|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                                  } |j        d t          j        |dd	dd	f                                         ||d
dd
df                                         fd
          D             d t          j        |dd	dd	f                                         ||d
dd
df                                         fd
          D             fi |  |j        d t          j        |dd	d
df                                         ||d
ddd	f                                         fd
          D             d t          j        |dd	d
df                                         ||d
ddd	f                                         fd
          D             fi | |A|                    |                                |                                |d|d           dS dS )a  Plot quad grid lines on a single plot.

        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 Bokeh Figure, optional): Which plot to use, 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 ``Category10`` palette.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        c                     g | ]}|S  rW   .0rows     r3   
<listcomp>z&BokehRenderer.grid.<locals>.<listcomp>q       ccr5   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>q       "6"6"633"6"6"6r5   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>r   r\   r5   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>r   r^   r5   )
line_colorrR   r   g      ?Nr   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>z       aaaaaar5   )r'   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>{   rd   r5   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>~   rd   r5   c                     g | ]}|S rW   rW   rX   s     r3   r[   z&BokehRenderer.grid.<locals>.<listcomp>   rd   r5      )rD   rE   
fill_colorra   rR   size)	r@   rF   Tdict
multi_lineravelr   stackcircle)r)   rD   rE   r?   r;   rR   point_colorquad_as_tri_alphar2   rJ   rK   kwargsxmidymids                 r3   gridzBokehRenderer.grid[   s   ( r""1%%1Q"6"6!#"6"6"66Q"6"6!#"6"6"66e444r2(((((q  	!CRC"H+!""crc'
2QssABBwZ?!ABBF)KLSSUUD!CRC"H+!""crc'
2QssABBwZ?!ABBF)KLSSUUDCNaa!CRC"H+*;*;*=*=tQqrr122vY__EVEV)W^_ ` ` `aaaaa!CRC"H+*;*;*=*=tQqrr122vY__EVEV)W^_ ` ` `aaa     CNaa!CRCG**:*:*<*<dAabb#2#gJDTDTDVDV)W^_ ` ` `aaaaa!CRCG**:*:*<*<dAabb#2#gJDTDTDVDV)W^_ ` ` `aaa      	bJJ''))qwwyyUtSX_`  b b b b b	b 	br5         ?c                     |                      |          }|                     |          }t          ||          \  }}	t          |          dk    r|                    ||	|||           dS dS )a  Plot contour lines on a single plot.

        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 Bokeh Figure, optional): Which plot to use, 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
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.

        Note:
            Assumes all lines are open line strips not closed line loops.
        r   )ra   
line_alpharM   N)r@   r=   r   rN   rm   )
r)   lines	line_typer?   r;   rR   	linewidthr2   rJ   rK   s
             r3   rz   zBokehRenderer.lines   s    $ r""##E**y11Br77Q; 	]NN2reR[N\\\\\	] 	]r5   c                 >   t           j                            |          }|t           j        j        u rdS |                     |          }|                     |          }|                     ||          \  }}|                    ||         ||         |d           dS )a  Plot masked out grid points as circles on a single plot.

        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 Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        Nr   )ri   rj   )r   magetmasknomaskr@   r=   rF   rp   )r)   rD   rE   zr?   r;   maskr2   s           r3   r   zBokehRenderer.mask   s     u}}Q25< 	Fr""##E**1%%1

1T7AdGB
?????r5   c                     |r| j         D ]}d|_        d|_        | j        rt	          | j        |           dS t          | j        |           dS )aO  Save plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.

        Warning:
            To output to SVG file, ``want_svg=True`` must have been passed to the constructor.
        N)filename)r    background_fill_colorborder_fill_colorr   r   r(   r   )r)   r   transparentr2   s       r3   savezBokehRenderer.save   sp      	-} - -,0)(,%%> 	8t|h777777t|h777777r5   c                     t          | j                  }t          j                    }|                    |d           |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        png)r   r(   ioBytesIOr   )r)   imagebuffers      r3   save_to_bufferzBokehRenderer.save_to_buffer   s7     &dl33

65!!!r5   c                 .    t          | j                   dS )z:Show plots in web browser, in usual Bokeh manner.
        N)r   r(   )r)   s    r3   r   zBokehRenderer.show   s     	T\r5   c                     |                      |          }||_        d|j        _        |!|                     |          |j        _        dS dS )a  Set the title of a single plot.

        Args:
            title (str): Title text.
            ax (int or Bokeh Figure, optional): Which plot 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
                ``Category10`` palette. Default ``None`` which is ``black``.
        centerN)r@   titlealignr=   
text_color)r)   r   r?   r;   r2   s        r3   r   zBokehRenderer.title   sW     r""	"	 	>#'#6#6u#=#=CI   	> 	>r5   green.1fc                 <   |                      |          }|                     |          }|                     ||          \  }}t          j        |          }|j        \  }	}
t          |dd          }t          |	          D ]R}t          |
          D ]@}|                    t          d|||f         |||f         |||f         | d|           AS|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                   }|                    t          d|||| d|           dS dS )	a  Show ``z`` values on a single plot.

        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 Bokeh Figure, optional): Which plot to use, 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
                ``Category10`` palette. 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`` centres
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        r   middle)r   
text_aligntext_baseline)rD   rE   textr      NrW   )r@   r=   rF   r   r   shaperl   r!   
add_layoutr   mean)r)   rD   rE   r   r?   r;   fmtquad_as_trir2   nynxrs   jixxyyzzs                    r3   z_valueszBokehRenderer.z_values   s   $ r""##E**1%%1JqMMB88TTTr 	_ 	_A2YY _ _u]qAw!AqD'1QT7C@R@R]]V\]]^^^^_ 	T2a4[[ T Tr!t T TA1QqS5!AaC%<11B1QqS5!AaC%<11B1QqS5!AaC%<11BNN5#R2R--#R#R6#R#RSSSS	T	T 	TT Tr5   )r   r   r   TF)r   rG   rH   )r   rS   rT   Nr   )r   rG   rw   r   )r   rS   )F)r   N)r   r   r   F)__name__
__module____qualname____doc__r4   r=   r@   rF   rP   rv   rz   r   r   r   r   r   r   rW   r5   r3   r   r      s$         I I I I,    
  ^ ^ ^ ^((b (b (b (bT] ] ] ]0@ @ @ @$8 8 8 8*	 	 	  
> > > > !T !T !T !T !T !Tr5   r   )r   bokeh.ior   r   r   bokeh.io.exportr   bokeh.layoutsr   bokeh.modelsr   bokeh.palettesr	   bokeh.plottingr
   numpyr   
bokeh_utilr   r   r   rW   r5   r3   <module>r      s    				 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 " " " " " "       % % % % % % ! ! ! ! ! !     7 7 7 7 7 7 7 7wT wT wT wT wT wT wT wT wT wTr5   