Example #1
0
    def draw_edges(self,
                   keys=None,
                   width=None,
                   color=None,
                   text=None,
                   textcolor=None,
                   fontsize=None):
        """Draws the mesh edges.

        Parameters
        ----------
        keys : list
            The keys of the edges to plot.
        width : {float, dict}
            Width of the mesh edges.
        color : {color, dict}
            Color for the edge lines.
        text : {{'index', 'key'}, str, dict}
            Strings to be displayed on the edges.
        textcolor : rgb tuple or dict of rgb tuples
            Color for the text to be displayed on the edges.
        fontsize : int or dict of int.
            Font size for the text to be displayed on the edges.

        Returns
        -------
        object
            The matplotlib edge collection object.

        """
        keys = keys or list(self.mesh.edges())

        if text == 'key':
            text = {(u, v): '{}-{}'.format(u, v) for u, v in self.mesh.edges()}
        elif text == 'index':
            text = {(u, v): str(index) for index, (u, v) in enumerate(self.mesh.edges())}
        else:
            pass

        widthdict = valuedict(keys, width, self.defaults['edge.width'])
        colordict = valuedict(keys, color, self.defaults['edge.color'])
        textdict = valuedict(keys, text, '')
        textcolordict = valuedict(keys, textcolor, self.defaults['edge.textcolor'])
        fontsizedict = valuedict(keys, fontsize, self.defaults['edge.fontsize'])

        lines = []
        for u, v in keys:
            lines.append({
                'start': self.mesh.vertex_coordinates(u, 'xy'),
                'end': self.mesh.vertex_coordinates(v, 'xy'),
                'width': widthdict[(u, v)],
                'color': colordict[(u, v)],
                'text': textdict[(u, v)],
                'textcolor': textcolordict[(u, v)],
                'fontsize': fontsizedict[(u, v)]
            })

        collection = self.draw_lines(lines)
        self.edgecollection = collection
        return collection
Example #2
0
    def update_vertices(self, radius=None):
        """Updates the plotter vertex collection based on the current state of the mesh.

        Parameters
        ----------
        radius : {float, dict}, optional
            The vertex radius as a single value, which will be applied to all vertices,
            or as a dictionary mapping vertex keys to specific radii.
            Default is the value set in ``self.defaults``.

        Notes
        -----
        This function will only work as expected if all vertices were already present in the collection.

        Examples
        --------
        .. code-block:: python

            pass

        """
        radius = valuedict(self.mesh.vertices(), radius, self.defaults['vertex.radius'])
        circles = []
        for key in self.mesh.vertices():
            c = self.mesh.vertex_coordinates(key, 'xy')
            r = radius[key]
            circles.append(Circle(c, r))
        self.vertexcollection.set_paths(circles)
Example #3
0
 def update_faces(self, facecolor=None):
     """Updates the plotter face collection based on the mesh."""
     facecolor = valuedict(self.mesh.faces(), facecolor, self.defaults['face.facecolor'])
     polygons = []
     facecolors = []
     for fkey in self.mesh.faces():
         points = self.mesh.face_coordinates(fkey, 'xy')
         polygons.append(Polygon(points))
         facecolors.append(color_to_rgb(facecolor[fkey], normalize=True))
     self.facecollection.set_paths(polygons)
     self.facecollection.set_facecolor(facecolors)
Example #4
0
    def draw_nodes(self,
                   keys=None,
                   radius=None,
                   text=None,
                   facecolor=None,
                   edgecolor=None,
                   edgewidth=None,
                   textcolor=None,
                   fontsize=None,
                   picker=None):
        """Draws the network nodes.

        Parameters
        ----------
        keys : list
            The keys of the nodes to plot.
        radius : {float, dict}
            A list of radii for the nodes.
        text : {{'index', 'key'}, str, dict}
            Strings to be displayed on the nodes.
        facecolor : {color, dict}
            Color for the node circle fill.
        edgecolor : {color, dict}
            Color for the node circle edge.
        edgewidth : {float, dict}
            Width for the node circle edge.
        textcolor : {color, dict}
            Color for the text to be displayed on the nodes.
        fontsize : {int, dict}
            Font size for the text to be displayed on the nodes.

        Returns
        -------
        object
            The matplotlib point collection object.

        """
        keys = keys or list(self.datastructure.nodes())

        if text == 'key':
            text = {key: str(key) for key in self.datastructure.nodes()}
        elif text == 'index':
            text = {
                key: str(index)
                for index, key in enumerate(self.datastructure.nodes())
            }
        elif isinstance(text, str):
            if text in self.datastructure.default_node_attributes:
                default = self.datastructure.default_node_attributes[text]
                if isinstance(default, float):
                    text = {
                        key: '{:.1f}'.format(attr[text])
                        for key, attr in self.datastructure.nodes(True)
                    }
                else:
                    text = {
                        key: str(attr[text])
                        for key, attr in self.datastructure.nodes(True)
                    }
        else:
            pass

        radiusdict = valuedict(keys, radius, self.defaults['node.radius'])
        textdict = valuedict(keys, text, '')
        facecolordict = valuedict(keys, facecolor,
                                  self.defaults['node.facecolor'])
        edgecolordict = valuedict(keys, edgecolor,
                                  self.defaults['node.edgecolor'])
        edgewidthdict = valuedict(keys, edgewidth,
                                  self.defaults['node.edgewidth'])
        textcolordict = valuedict(keys, textcolor,
                                  self.defaults['node.textcolor'])
        fontsizedict = valuedict(keys, fontsize,
                                 self.defaults['node.fontsize'])

        points = []
        for key in keys:
            points.append({
                'pos': self.datastructure.node_coordinates(key, 'xy'),
                'radius': radiusdict[key],
                'text': textdict[key],
                'facecolor': facecolordict[key],
                'edgecolor': edgecolordict[key],
                'edgewidth': edgewidthdict[key],
                'textcolor': textcolordict[key],
                'fontsize': fontsizedict[key]
            })

        collection = self.draw_points(points)
        self.nodecollection = collection

        if picker:
            collection.set_picker(picker)
        return collection
Example #5
0
    def draw_faces(self,
                   keys=None,
                   text=None,
                   facecolor=None,
                   edgecolor=None,
                   edgewidth=None,
                   textcolor=None,
                   fontsize=None):
        """Draws the mesh faces.

        Parameters
        ----------
        keys : list
            The keys of the edges to plot.
        text : {{'index', 'key'}, str, dict}
            Strings to be displayed on the edges.
        facecolor : {color, dict}
            Color for the face fill.
        edgecolor : {color, dict}
            Color for the face edge.
        edgewidth : {float, dict}
            Width for the face edge.
        textcolor : {color, dict}
            Color for the text to be displayed on the edges.
        fontsize : {int, dict}
            Font size for the text to be displayed on the edges.

        Returns
        -------
        object
            The matplotlib face collection object.
        """
        keys = keys or list(self.mesh.faces())

        if text == 'key':
            text = {key: str(key) for key in self.mesh.faces()}
        elif text == 'index':
            text = {key: str(index) for index, key in enumerate(self.mesh.faces())}
        else:
            pass

        textdict = valuedict(keys, text, '')
        facecolordict = valuedict(keys, facecolor, self.defaults['face.facecolor'])
        edgecolordict = valuedict(keys, edgecolor, self.defaults['face.edgecolor'])
        edgewidthdict = valuedict(keys, edgewidth, self.defaults['face.edgewidth'])
        textcolordict = valuedict(keys, textcolor, self.defaults['face.textcolor'])
        fontsizedict = valuedict(keys, fontsize, self.defaults['face.fontsize'])

        polygons = []
        for key in keys:
            polygons.append({
                'points': self.mesh.face_coordinates(key, 'xy'),
                'text': textdict[key],
                'facecolor': facecolordict[key],
                'edgecolor': edgecolordict[key],
                'edgewidth': edgewidthdict[key],
                'textcolor': textcolordict[key],
                'fontsize': fontsizedict[key]
            })

        collection = self.draw_polygons(polygons)
        self.facecollection = collection
        return collection