def bbox(self, bbox): """Sets the bounding box of the drawing area where this drawer will draw.""" if not isinstance(bbox, BoundingBox): self._bbox = BoundingBox(bbox) else: self._bbox = bbox
def add(self, obj, bbox=None, palette=None, opacity=1.0, *args, **kwds): """Adds an object to the plot. Arguments not specified here are stored and passed to the object's plotting function when necessary. Since you are most likely interested in the arguments acceptable by graphs, see L{Graph.__plot__} for more details. @param obj: the object to be added @param bbox: the bounding box of the object. If C{None}, the object will fill the entire area of the plot. @param palette: the color palette used for drawing the object. If the object tries to get a color assigned to a positive integer, it will use this palette. If C{None}, defaults to the global palette of the plot. @param opacity: the opacity of the object being plotted, in the range 0.0-1.0 @see: Graph.__plot__ """ if opacity < 0.0 or opacity > 1.0: raise ValueError("opacity must be between 0.0 and 1.0") if bbox is None: bbox = self.bbox if not isinstance(bbox, BoundingBox): bbox = BoundingBox(bbox) self._objects.append((obj, bbox, palette, opacity, args, kwds)) self.mark_dirty()
def bounding_box(self, border=0): """Returns the bounding box of the layout. The bounding box of the layout is the smallest box enclosing all the points in the layout. @param border: this value gets subtracted from the minimum bounds and gets added to the maximum bounds before returning the coordinates of the box. Defaults to zero. @return: the coordinates of the lower left and the upper right corner of the box. "Lower left" means the minimum coordinates and "upper right" means the maximum. These are encapsulated in a L{BoundingBox} object. """ if self._dim != 2: raise ValueError("Layout.boundary_box() supports 2D layouts only") try: (x0, y0), (x1, y1) = self.boundaries(border) return BoundingBox(x0, y0, x1, y1) except ValueError: return BoundingBox(0, 0, 0, 0)
def __init__(self, target=None, bbox=None, palette=None, background=None): """Creates a new plot. @param target: the target surface to write to. It can be one of the following types: - C{None} -- an appropriate surface will be created and the object will be plotted there. - C{cairo.Surface} -- the given Cairo surface will be used. - C{string} -- a file with the given name will be created and an appropriate Cairo surface will be attached to it. @param bbox: the bounding box of the surface. It is interpreted differently with different surfaces: PDF and PS surfaces will treat it as points (1 point = 1/72 inch). Image surfaces will treat it as pixels. SVG surfaces will treat it as an abstract unit, but it will mostly be interpreted as pixels when viewing the SVG file in Firefox. @param 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. @param background: the background color. If C{None}, the background will be transparent. You can use any color specification here that is understood by L{igraph.drawing.colors.color_name_to_rgba}. """ self._filename = None self._surface_was_created = not isinstance(target, cairo.Surface) self._need_tmpfile = False # Several Windows-specific hacks will be used from now on, thanks # to Dale Hunscher for debugging and fixing all that stuff self._windows_hacks = "Windows" in platform.platform() if bbox is None: self.bbox = BoundingBox(600, 600) elif isinstance(bbox, tuple) or isinstance(bbox, list): self.bbox = BoundingBox(bbox) else: self.bbox = bbox if palette is None: config = Configuration.instance() palette = config["plotting.palette"] if not isinstance(palette, Palette): palette = palettes[palette] self._palette = palette if target is None: self._need_tmpfile = True self._surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, int(self.bbox.width), int(self.bbox.height) ) elif isinstance(target, cairo.Surface): self._surface = target else: self._filename = target _, ext = os.path.splitext(target) ext = ext.lower() if ext == ".pdf": self._surface = cairo.PDFSurface( target, self.bbox.width, self.bbox.height ) elif ext == ".ps" or ext == ".eps": self._surface = cairo.PSSurface( target, self.bbox.width, self.bbox.height ) elif ext == ".png": self._surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, int(self.bbox.width), int(self.bbox.height) ) elif ext == ".svg": self._surface = cairo.SVGSurface( target, self.bbox.width, self.bbox.height ) else: raise ValueError("image format not handled by Cairo: %s" % ext) self._ctx = cairo.Context(self._surface) self._objects = [] self._is_dirty = False self.background = background
def plot(obj, target=None, bbox=(0, 0, 600, 600), *args, **kwds): """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 an appropriate Cairo surface will be attached to it. The supported image formats are: PNG, PDF, SVG and PostScript. - 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} -- a temporary file will be created and the object will be plotted there. igraph will attempt to open an image viewer and show the temporary file. This feature is deprecated from python-igraph version 0.9.1 and will be removed in 0.10.0. @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. @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}. @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 on each side. @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{Plot} object. @see: Graph.__plot__ """ _, plt = find_matplotlib() if hasattr(plt, "Axes") and isinstance(target, plt.Axes): result = MatplotlibGraphDrawer(ax=target) result.draw(obj, *args, **kwds) return if not isinstance(bbox, BoundingBox): bbox = BoundingBox(bbox) result = Plot(target, bbox, background=kwds.get("background", "white")) if "margin" in kwds: bbox = bbox.contract(kwds["margin"]) del kwds["margin"] else: bbox = bbox.contract(20) result.add(obj, bbox, *args, **kwds) if target is None and _is_running_in_ipython(): # Get the default value of the `inline` argument from the configuration if # needed inline = kwds.get("inline") if inline is None: config = Configuration.instance() inline = config["shell.ipython.inlining.Plot"] # If we requested an inline plot, just return the result and IPython will # call its _repr_svg_ method. If we requested a non-inline plot, show the # plot in a separate window and return nothing if inline: return result else: result.show() return # We are either not in IPython or the user specified an explicit plot target, # so just show or save the result if target is None: result.show() elif isinstance(target, str): result.save() # Also return the plot itself return result
def bounding_box(self): """Returns the bounding box of the Cairo surface as a L{BoundingBox} object""" return BoundingBox(self.bbox)
def bounds(self, bounds): """Sets the lower and upper bounds of the X and Y values""" self._bounds = BoundingBox(bounds) self._recalc_scale_factors()
def bbox(self): """Returns the bounding box of the coordinate system""" return BoundingBox(self._bbox.coords)
def plot(obj, target=None, bbox=(0, 0, 600, 600), *args, **kwds): """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{None} -- an appropriate surface will be created and the object will be plotted there. - 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{string} -- a file with the given name will be created and an appropriate Cairo surface will be attached to it. The supported image formats are: PNG, PDF, SVG and PostScript. @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. @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}. @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 on each side. @keyword inline: whether to try and show the plot object inline in the current IPython notebook. Passing ``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{Plot} object. @see: Graph.__plot__ """ if not isinstance(bbox, BoundingBox): bbox = BoundingBox(bbox) result = Plot(target, bbox, background="white") if "margin" in kwds: bbox = bbox.contract(kwds["margin"]) del kwds["margin"] else: bbox = bbox.contract(20) result.add(obj, bbox, *args, **kwds) if IN_IPYTHON and target is None: # Get the default value of the `inline` argument from the configuration if # needed inline = kwds.get("inline") if inline is None: config = Configuration.instance() inline = config["shell.ipython.inlining.Plot"] # If we requested an inline plot, just return the result and IPython will # call its _repr_svg_ method. If we requested a non-inline plot, show the # plot in a separate window and return nothing if inline: return result else: result.show() return # We are either not in IPython or the user specified an explicit plot target, # so just show or save the result if target is None: result.show() elif isinstance(target, basestring): result.save() # Also return the plot itself return result
def plot(obj, target=None, bbox=(0, 0, 600, 600), *args, **kwds): """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{None} -- an appropriate surface will be created and the object will be plotted there. - 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{string} -- a file with the given name will be created and an appropriate Cairo surface will be attached to it. The supported image formats are: PNG, PDF, SVG and PostScript. @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. @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}. @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 on each side. @return: an appropriate L{Plot} object. @see: Graph.__plot__ """ if not isinstance(bbox, BoundingBox): bbox = BoundingBox(bbox) result = Plot(target, bbox, background="white") if "margin" in kwds: bbox = bbox.contract(kwds["margin"]) del kwds["margin"] else: bbox = bbox.contract(20) result.add(obj, bbox, *args, **kwds) if target is None: result.show() if isinstance(target, basestring): result.save() return result