U
    td9/                     @   s  d 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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# ddl$m%Z%m&Z&m'Z' ddl(m)Z) dZ*eZ+eZ,G dd dZ-dddZ.dS )ad  
Drawing and plotting routines for igraph.

IGraph has two stable plotting backends at the moment: Cairo and Matplotlib.
It also has experimental support for plotly.

The Cairo backend is dependent on the C{pycairo} or C{cairocffi} libraries that
provide Python bindings to the popular U{Cairo library<http://www.cairographics.org>}.
This means that if you don't have U{pycairo<http://www.cairographics.org/pycairo>}
or U{cairocffi<http://cairocffi.readthedocs.io>} installed, you won't be able
to use the Cairo plotting backend. Whenever the documentation refers to the
C{pycairo} library, you can safely replace it with C{cairocffi} as the two are
API-compatible.

The Matplotlib backend uses the U{Matplotlib library<https://matplotlib.org>}.
You will need to install it from PyPI if you want to use the Matplotlib
plotting backend. Many of our gallery examples use the matplotlib backend.

The plotly backend uses the U{plotly library <https://plotly.com/python/>} and,
like matplotlib, requires installation from PyPI.

If you do not want to (or cannot) install any of the dependencies outlined
above, you can still save the graph to an SVG file and view it from
U{Mozilla Firefox<http://www.mozilla.org/firefox>} (free) or edit it in
U{Inkscape<http://www.inkscape.org>} (free), U{Skencil<http://www.skencil.org>}
(formerly known as Sketch, also free) or Adobe Illustrator.
    )Path)warn)Configuration)
find_cairo)find_matplotlib)find_plotly)	CairoPlot)Palettepalettes)CairoGraphDrawer)CairoMatrixDrawer)CairoHistogramDrawer)CairoPaletteDrawer)MatplotlibGraphDrawer)MatplotlibMatrixDrawer)MatplotlibHistogramDrawer)MatplotlibPaletteDrawer)PlotlyGraphDrawer)BoundingBoxPoint	Rectangle)_is_running_in_ipython)	r   r   r   DefaultGraphDrawerPlotr   r   plotDrawerDirectoryc                   @   sH   e Zd ZdZdZdZeeee	de
eeeddeidZedd ZdS )	r   zmStatic class that finds the object/backend drawer

    This directory is used by the __plot__ functions.
    )cairo
matplotlib)GraphZMatrixZ	Histogramr	   r   r   r   plotlyc                 C   sX   t |jdd d}z| j| | W S  tk
rR   td| d| Y nX dS )a]  Given a shape name, returns the corresponding shape drawer class

        @param cls: the class to resolve
        @param obj: an instance of the object to plot
        @param backend: the name of the backend
        @return: the corresponding shape drawer class

        @raise ValueError: if no drawer is available for this backend/object
        .z<'>zunknown drawer for z and backend N)str	__class__splitstripknown_drawersKeyError
ValueError)clsobjbackendZobject_name r-   P/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/__init__.pyresolveg   s    zDrawerDirectory.resolveN)__name__
__module____qualname____doc__Zvalid_backendsZvalid_objectsr   r   r   r   r   r   r   r   r   r'   classmethodr/   r-   r-   r-   r.   r   H   s&    r   Nr   r   X  r6   c                 O   sd  d}t  \}}t }t }	t|dr6t||jr6d}
nFt|	drTt||	jjrTd}
n(t|drpt||jrpd}
nt	
 d }
|
|krtd	|
|
d
kr|dd}|dkrt| dt	
 d }|dk	rt|tst| }t|ttfrt|}d}nd}|dkr4|
dkr | \}}n|
dkr4|	j }t| dd}|dkr\td| f  dS ||
|f|d|i| |dk	r|
dkr|j| n|
dkr|| |S d}|dkrt r|d}|dkrt	
 d }|dd}|dd}t|dd}t||||d}|j|}|j| |f|| |rH|S t|ttfr`|  |S )a  Plots the given object to the given target.

    Positional and keyword arguments not explicitly mentioned here will be
    passed down to the C{__plot__} method of the object being plotted.
    Since you are most likely interested in the keyword arguments available
    for graph plots, see L{Graph.__plot__} as well.

    @param obj: the object to be plotted
    @param target: the target where the object should be plotted. It can be one
      of the following types:

        - C{matplotib.axes.Axes} -- a matplotlib/pyplot axes in which the
          graph will be plotted. Drawing is delegated to the chosen matplotlib
          backend, and you can use interactive backends and matplotlib
          functions to save to file as well.

        - C{string} -- a file with the given name will be created and the plot
          will be stored there. If you are using the Cairo backend, an
          appropriate Cairo surface will be attached to the file. If you are
          using the matplotlib backend, the Figure will be saved to that file
          using Figure.savefig with default parameters. The supported image
          formats for Cairo are: PNG, PDF, SVG and PostScript; matplotlib might
          support additional formats.

        - C{cairo.Surface} -- the given Cairo surface will be used. This can
          refer to a PNG image, an arbitrary window, an SVG file, anything that
          Cairo can handle.

        - C{None} -- If you are using the Cairo backend, no plotting will be
          performed; igraph simply returns a CairoPlot_ object that you can use
          to manipulate the plot and save it to a file later. If you are using
          the matplotlib backend, a Figure objet and an Axes are created and
          the Axes is returned so you can manipulate it further. Similarly, if
          you are using the plotly backend, a Figure object is returned.

    @param bbox: the bounding box of the plot. It must be a tuple with either
      two or four integers, or a L{BoundingBox} object. If this is a tuple
      with two integers, it is interpreted as the width and height of the plot
      (in pixels for PNG images and on-screen plots, or in points for PDF,
      SVG and PostScript plots, where 72 pt = 1 inch = 2.54 cm). If this is
      a tuple with four integers, the first two denotes the X and Y coordinates
      of a corner and the latter two denoting the X and Y coordinates of the
      opposite corner. Ignored for Matplotlib plots.

    @keyword opacity: the opacity of the object being plotted. It can be
      used to overlap several plots of the same graph if you use the same
      layout for them -- for instance, you might plot a graph with opacity
      0.5 and then plot its spanning tree over it with opacity 0.1. To
      achieve this, you'll need to modify the L{Plot} object returned with
      L{Plot.add}. Ignored for Matplotlib plots.

    @keyword palette: the palette primarily used on the plot if the
      added objects do not specify a private palette. Must be either
      an L{igraph.drawing.colors.Palette} object or a string referring
      to a valid key of C{igraph.drawing.colors.palettes} (see module
      L{igraph.drawing.colors}) or C{None}. In the latter case, the default
      palette given by the configuration key C{plotting.palette} is used.

    @keyword margin: the top, right, bottom, left margins as a 4-tuple.
      If it has less than 4 elements or is a single float, the elements
      will be re-used until the length is at least 4. The default margin
      is 20 units on each side. Ignored for Matplotlib plots.

    @keyword inline: whether to try and show the plot object inline in the
      current IPython notebook. Passing C{None} here or omitting this keyword
      argument will look up the preferred behaviour from the
      C{shell.ipython.inlining.Plot} configuration key.  Note that this keyword
      argument has an effect only if igraph is run inside IPython and C{target}
      is C{None}.

    @return: an appropriate L{CairoPlot} object for the Cairo backend, the
      Matplotlib C{Axes} object for the Matplotlib backend, and the C{Figure}
      object for the plotly backend.

    @see: Graph.__plot__
    r   Axesr   graph_objectsr    Surfacer   zplotting.backendzunknown plotting backend: {0!r})r   r    paletteNZ_default_palettezplotting.paletteZ__plot__z%s does not support plottingFinlinezshell.ipython.inlining.Plot
backgroundwhitemargin   )targetbboxr:   r<   ) r   r   r   hasattr
isinstancer7   r8   ZFigurer9   r   instancer)   formatpopgetattrr	   r
   r#   r   Zsubplotsr   ZfigureZsavefigZwrite_imager   getfloatr   rA   Zcontractaddsave)r+   r@   rA   argskwdsZVALID_BACKENDS_Zpltr   r    r,   r:   Z	save_pathZplotterr;   r<   r>   resultZ	item_bboxr-   r-   r.   r   |   s    N
 












r   )Nr5   )/r3   pathlibr   warningsr   Zigraph.configurationr   Zigraph.drawing.cairo.utilsr   Zigraph.drawing.matplotlib.utilsr   Zigraph.drawing.plotly.utilsr   Zigraph.drawing.cairo.plotr   Zigraph.drawing.colorsr	   r
   Zigraph.drawing.cairo.graphr   Zigraph.drawing.cairo.matrixr   Zigraph.drawing.cairo.histogramr   Zigraph.drawing.cairo.paletter   Zigraph.drawing.matplotlib.graphr   Z igraph.drawing.matplotlib.matrixr   Z#igraph.drawing.matplotlib.histogramr   Z!igraph.drawing.matplotlib.paletter   Zigraph.drawing.plotly.graphr   Zigraph.drawing.utilsr   r   r   Zigraph.utilsr   __all__r   r   r   r   r-   r-   r-   r.   <module>   s0   4