
    MhZ,                     
   d Z ddl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mZ dd	lmZ dd
lmZ ddlmZ ddlmZ e
ddZd ZddZde_        g e_        d ZeZd Zd Z e             d Zd Zd Z d Z!ddZ"dS )z<A matplotlib backend for publishing figures via display_data    N)colors)backend_agg)FigureCanvasAgg)Gcf)Figure)InteractiveShell)get_ipython)select_figure_formats)display   )InlineBackend)FigureClassc                .    t          |  ||i |          S )z
    Return a new figure manager for a new figure instance.

    This function is part of the API expected by Matplotlib backends.
    )new_figure_manager_given_figure)numr   argskwargss       `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/matplotlib_inline/backend_inline.pynew_figure_managerr      s#     +3T0LV0L0LMMM    c                 H   t          j        |           }t          d          s
fd_        t	          j                    s|S 	 t          j                                       n# t          $ r Y nw xY wt          j        	                               dt          _
        |S )z
    Return a new figure manager for a given figure instance.

    This function is part of the API expected by Matplotlib backends.
    showc                  @    t          t                              S )Nmetadata)r   _fetch_figure_metadata)afigures    r   <lambda>z1new_figure_manager_given_figure.<locals>.<lambda>0   s$    3F;;"= "= "= r   T)r   r   hasattrr   
matplotlibis_interactive_to_drawremove
ValueErrorappend_draw_called)r   r   managers    ` r   r   r      s     9#vFFG 66"" == = = = $&& 
V$$$$    	M   DNs   A' '
A43A4c                    | t          j                    j        } 	 t          j                    D ]4}t          |j        j        t          |j        j                             5	 g t          _
        | r4t          j                    r#t          j                            d           dS dS dS # g t          _
        | r3t          j                    r!t          j                            d           w w w xY w)a  Show all figures as SVG/PNG payloads sent to the IPython clients.

    Parameters
    ----------
    close : bool, optional
        If true, a ``plt.close('all')`` call is automatically issued after
        sending all the figures. If this is set, the figures will entirely
        removed from the internal list of figures.
    block : Not used.
        The `block` parameter is a Matplotlib experimental parameter.
        We accept it in the function signature for compatibility with other
        backends.
    Nr   all)r   instanceclose_figuresr   get_all_fig_managersr   canvasr   r   r   r#   r!   pyplotclose)r0   blockfigure_managers      r   r   r   H   s    }&((6+!688 	 	N%,/0E0LMM    	   	+S-// 	+##E*****	+ 	+ 	+ 	+   	+S-// 	+##E****	+ 	+s   AB+ +AC/Fc                  4   t           j        sdS 	 t          j                    j        r~	 t          d          g t           _        dt           _        S # t          $ rI} t                      }|| |                                 Y d} ~ g t           _        dt           _        dS d} ~ ww xY wt          d t          j                    D                       fdt           j        D             D ]x}	 t          |t          |                     "# t          $ rJ} t                      }|| |                                 Y d} ~  g t           _        dt           _        dS d} ~ ww xY w	 g t           _        dt           _        dS # g t           _        dt           _        w xY w)a  Send all figures that changed

    This is meant to be called automatically and will call show() if, during
    prior code execution, there had been any calls to draw_if_interactive.

    This function is meant to be used as a post_execute callback in IPython,
    so user-caused errors are handled with showtraceback() instead of being
    allowed to raise.  If this function is not called from within IPython,
    then these exceptions will raise.
    NTFc                 &    g | ]}|j         j        S  )r.   r   ).0fms     r   
<listcomp>z!flush_figures.<locals>.<listcomp>   s    LLL2bi&LLLr   c                     g | ]}|v |	S r5   r5   )r6   figactives     r   r8   z!flush_figures.<locals>.<listcomp>   s    BBBCC6MMCMMMr   r   )r   r'   r   r+   r,   r#   	Exceptionr	   showtracebacksetr   r-   r   r   )eipr:   r;   s      @r   flush_figuresrA   l   s     "!##1 		Dzz2 !3     ]]:G$$&&&FFF" !3 LL1I1K1KLLLMMBBBB4=BBB 
	 
	C	&<S&A&ABBBBB    ]]:G$$&&&FFFF !
	 ! !!!!!sY   E= A 
B$&BE= B$$AE= ,D
E= 
E&E;E= EE= =Fc                    t          j        |           }| |_        || j        vr| j                            |           |dv rr| j                            dt                     i | _        |j	        D ]}t          j        |         | j        |<   t          j                            |j	                   d}nj	 | j                            dt                     n# t          $ r Y nw xY wt          | d          r&t          j                            | j                   | `d}t!          t"          dd          }||k    r&t%          | |j        fi |j         |t"          _        d	S d	S )
zConfigure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend
    )parentinlinez)module://matplotlib_inline.backend_inlinepost_executerE   _saved_rcParamsothercurrent_backendunsetN)r   r+   shellconfigurablesr&   eventsregisterrA   rG   rcr!   rcParamsupdate
unregisterr%   r    getattrconfigure_inline_supportr
   figure_formatsprint_figure_kwargsrI   )rK   backendcfgknew_backend_namecur_backends         r   rT   rT      s    
 
.
.
.CCI
%%%%""3'''IIInm<<< !# 	> 	>A'1':1'=E!!$$""36***#	L##NMBBBB 	 	 	D	5+,, 	&&&u'<===%"
 24EwOOK;&&eS%7SS3;RSSS3C 000 '&s   7 C 
C%$C%c                     ddl m}  t                       |             rcdv raddlm 	             t                     dS # t          t          f$ r' fdj        	                    d           Y dS w xY wdS dS )zYEnable extra IPython matplotlib integration when we are loaded as the matplotlib backend.r   )get_backendrD   )activate_matplotlibc                  t                t                     j                            d           d S )Npost_run_cell)rT   rM   rR   )r   r^   rW   configure_oncer@   s    r   ra   z6_enable_matplotlib_integration.<locals>.configure_once   sB    ##G,,,(W555	$$_nEEEEEr   r`   N)
r!   r]   r	   IPython.core.pylabtoolsr^   rT   ImportErrorAttributeErrorrM   rN   )r]   r^   rW   ra   r@   s    @@@@r   _enable_matplotlib_integrationre      s   &&&&&&	BkmmG	 @gPPP??????		@((($R11111^, 	@ 	@ 	@F F F F F F F F I??????	@@ @PPs   A 4BBc                     t          |                                           rQt          d | j        D                       }|j        r,||d         k                                    rd|d         rdndiS dS )z3Get some metadata to help with displaying a figure.c                     g | ]>}|j         |j        fD ]-}|                                D ]}|                                .?S r5   )xaxisyaxisget_ticklabels	get_color)r6   axesaxislabels       r   r8   z*_fetch_figure_metadata.<locals>.<listcomp>   s      D  D  D$(-1Z,D D  D$(-1-@-@-B-B D  D %* !& 1 1  D  D  D  D  Dr   r   needs_backgrounddarklightN)_is_transparentget_facecolor	_is_lightrl   sizer*   )r:   
ticksLights     r   r   r      s     s((**++ N  D  D,/H D  D  D E E
 ? 	N
jm ;@@BB 	N&*Q-(LWMM4r   c                 t    t          j        |           }|ddddf                             d          dk    S )zDetermines if a color (or each of a sequence of colors) is light (as
    opposed to dark). Based on ITU BT.601 luminance formula (see
    https://stackoverflow.com/a/596241).N   )gA`"?gbX9?gv/?      ?)r   to_rgba_arraydot)colorrgbaArrs     r   rt   rt      s>     "5))G111bqb5>011B66r   c                 B    t          j        |           }|d         dk     S )z"Determine transparency from alpha.rx   ry   )r   to_rgba)r|   rgbas     r   rr   rr      s    >%  D7R<r   c                      t          j                    }i }|                    |j                    |j        di | t	          j                    }t          || fi | dS )a}  Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::

        c.InlineBackend.figure_formats = {'png', 'jpeg'}
        c.InlineBackend.print_figure_kwargs.update({'quality' : 90})

    Parameters
    ----------
    *formats : strs
        One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs
        Keyword args will be relayed to ``figure.canvas.print_figure``.
    Nr5   )r   r+   rQ   rV   r   r
   )formatsr   rX   kwrK   s        r   set_matplotlib_formatsr     sp    ( 
 
"
"C	BIIc%&&&BI%''E%//B/////r   Tc                 :    t          j                    }| |_        dS )a  Set whether the inline backend closes all figures automatically or not.

    By default, the inline backend used in the IPython Notebook will close all
    matplotlib figures automatically after each cell is run. This means that
    plots in different cells won't interfere. Sometimes, you may want to make
    a plot in one cell and then refine it in later cells. This can be accomplished
    by::

        In [1]: set_matplotlib_close(False)

    To set this in your config files use the following::

        c.InlineBackend.close_figures = False

    Parameters
    ----------
    close : bool
        Should all matplotlib figures be automatically closed after each cell is
        run?
    N)r   r+   r,   )r0   rX   s     r   set_matplotlib_closer   !  s     * 
 
"
"CCr   )NN)T)#__doc__r!   r   matplotlib.backendsr   matplotlib.backends.backend_aggr   matplotlib._pylab_helpersr   matplotlib.figurer   IPython.core.interactiveshellr   IPython.core.getipythonr	   rb   r
   IPython.displayr   configr   r   r   r   r'   r#   rA   FigureCanvasrT   re   r   rt   rr   r   r   r5   r   r   <module>r      s   B B
           + + + + + + ; ; ; ; ; ; ) ) ) ) ) ) $ $ $ $ $ $ : : : : : : / / / / / / 9 9 9 9 9 9 # # # # # # ! ! ! ! ! ! 06 N N N N N' ' 'T+ + + +>  ," ," ,"d -D -D -D`@ @ @&           7 7 7  0 0 08     r   