Ticket #14177: trac_14177-graph-color_by_labels-nt.patch

File trac_14177-graph-color_by_labels-nt.patch, 9.9 KB (added by nthiery, 6 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1361822495 18000
    # Node ID d5e47e3512f87ecccdaefbc3368d4ddaf809c95e
    # Parent  5e67215196ed20eccf66855f7baac7cf14dd4f2a
    #14177: More uniform handling of color_by_labels for graph plot, plot3d, graphviz
    
    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b class GenericGraph(GenericGraph_pyx): 
    1431114311        - ``iterations`` - how many iterations of the spring layout
    1431214312          algorithm to go through, if applicable
    1431314313
    14314         - ``color_by_label`` - if True, color edges by their labels
     14314        - ``color_by_label`` - a boolean or dictionary or function (default: False)
     14315           whether to color each edge with a different color according
     14316           to its label; the colors are chosen along a rainbow, unless
     14317           they are specified by a function or dictionary mapping
     14318           labels to colors; this option is incompatible with
     14319           ``edge_color`` and ``edge_colors``.
    1431514320
    1431614321        - ``heights`` - if specified, this is a dictionary from a set
    1431714322          of floating point heights to a set of vertices
    class GenericGraph(GenericGraph_pyx): 
    1450814513            ...     D.add_edge((i,i+1,'a'))
    1450914514            ...     D.add_edge((i,i-1,'b'))
    1451014515            sage: D.plot(edge_labels=True,edge_colors=D._color_by_label())
     14516            sage: D.plot(edge_labels=True, color_by_label={'a':'blue', 'b':'red'}, edge_style='dashed')
    1451114517
    1451214518            sage: g = Graph({}, loops=True, multiedges=True,sparse=True)
    1451314519            sage: g.add_edges([(0,0,'a'),(0,0,'b'),(0,1,'c'),(0,1,'d'),
    class GenericGraph(GenericGraph_pyx): 
    1452514531
    1452614532            sage: G=Graph({'a':['a','b','b','b','e'],'b':['c','d','e'],'c':['c','d','d','d'],'d':['e']},sparse=True)
    1452714533            sage: G.show(pos={'a':[0,1],'b':[1,1],'c':[2,0],'d':[1,0],'e':[0,0]})
     14534
     14535        TESTS::
     14536
     14537            sage: G = DiGraph({0:{1:'a', 2:'a'}, 1:{0:'b'}, 2:{0:'c'}})
     14538            sage: p = G.plot(edge_labels=True, color_by_label={'a':'yellow', 'b':'purple'}); p
     14539            sage: sorted([x.options()['rgbcolor'] for x in p if isinstance(x, sage.plot.arrow.CurveArrow)])
     14540            ['black', 'purple', 'yellow', 'yellow']
    1452814541        """
    1452914542        return self.graphplot(**options).plot()
    1453014543
    class GenericGraph(GenericGraph_pyx): 
    1455914572
    1456014573        return self.graphplot(**plot_kwds).show(**kwds)
    1456114574
    14562     def plot3d(self, bgcolor=(1,1,1), vertex_colors=None, vertex_size=0.06,
     14575    def plot3d(self, bgcolor=(1,1,1),
     14576                     vertex_colors=None, vertex_size=0.06, vertex_labels=False,
    1456314577                     edge_colors=None, edge_size=0.02, edge_size2=0.0325,
    1456414578                     pos3d=None, color_by_label=False,
    1456514579                     engine='jmol', **kwds):
    class GenericGraph(GenericGraph_pyx): 
    1457514589
    1457614590        -  ``vertex_size`` - float (default: 0.06)
    1457714591
     14592        -  ``vertex_labels`` -- a boolean (default: False)
     14593           whether to display vertices using text labels instead of spheres
     14594
    1457814595        -  ``vertex_colors`` - optional dictionary to specify
    1457914596           vertex colors: each key is a color recognizable by tachyon (rgb
    1458014597           tuple (default: (1,0,0))), and each corresponding entry is a list
    class GenericGraph(GenericGraph_pyx): 
    1458414601        -  ``edge_colors`` - a dictionary specifying edge
    1458514602           colors: each key is a color recognized by tachyon ( default:
    1458614603           (0,0,0) ), and each entry is a list of edges.
    14587        
     14604
     14605        - ``color_by_label`` - a boolean or dictionary or function (default: False)
     14606           whether to color each edge with a different color according
     14607           to its label; the colors are chosen along a rainbow, unless
     14608           they are specified by a function or dictionary mapping
     14609           labels to colors; this option is incompatible with
     14610           ``edge_color`` and ``edge_colors``.
     14611
    1458814612        -  ``edge_size`` - float (default: 0.02)
    1458914613       
    1459014614        -  ``edge_size2`` - float (default: 0.0325), used for
    class GenericGraph(GenericGraph_pyx): 
    1466714691            ...
    1466814692            NotImplementedError: 3D plotting of multiple edges or loops not implemented.
    1466914693
     14694        TESTS::
     14695
     14696            sage: G = DiGraph({0:{1:'a', 2:'a'}, 1:{0:'b'}, 2:{0:'c'}})
     14697            sage: p = G.plot3d(edge_labels=True, color_by_label={'a':'yellow', 'b':'cyan'})
     14698            sage: s = p.x3d_str()
     14699
     14700        This 3D plot contains four yellow objects (two cylinders and
     14701        two cones), two black objects and 2 cyan objects::
     14702
     14703            sage: s.count("Material diffuseColor='1.0 1.0 0.0'")
     14704            4
     14705            sage: s.count("Material diffuseColor='0.0 0.0 0.0'")
     14706            2
     14707            sage: s.count("Material diffuseColor='0.0 1.0 1.0'")
     14708            2
     14709
     14710        .. SEEALSO::
     14711
     14712            - :meth:`plot`
     14713            - :meth:`graphviz_string`
     14714            - :meth:`_color_by_label`
    1467014715        """
    1467114716        import graph_plot
    1467214717        layout_options = dict( (key,kwds[key]) for key in kwds.keys() if key     in graph_plot.layout_options )
    class GenericGraph(GenericGraph_pyx): 
    1467714722        if self.has_multiple_edges() or self.has_loops():
    1467814723            raise NotImplementedError("3D plotting of multiple edges or loops not implemented.")
    1467914724        if engine == 'jmol':
    14680             from sage.plot.plot3d.all import sphere, line3d, arrow3d
     14725            from sage.plot.plot3d.all import sphere, line3d, arrow3d, text3d
    1468114726            from sage.plot.plot3d.texture import Texture
    1468214727            kwds.setdefault('aspect_ratio', [1,1,1])
    1468314728            verts = self.vertices()
    class GenericGraph(GenericGraph_pyx): 
    1468814733            if color_by_label:
    1468914734                if edge_colors is  None:
    1469014735                        # do the coloring
    14691                         edge_colors = self._color_by_label(format='rgbtuple')
     14736                        edge_colors = self._color_by_label(format=color_by_label)
    1469214737            elif edge_colors is None:
    1469314738                edge_colors = { (0,0,0) : self.edges() }
    1469414739
    class GenericGraph(GenericGraph_pyx): 
    1470314748                for color in vertex_colors:
    1470414749                    texture = Texture(color=color, ambient=0.1, diffuse=0.9, specular=0.03)
    1470514750                    for v in vertex_colors[color]:
    14706                         graphic += sphere(center=pos3d[v], size=vertex_size, texture=texture, **kwds)
     14751                        if vertex_labels:
     14752                            graphic += text3d(repr(v), pos3d[v])
     14753                        else:
     14754                            graphic += sphere(center=pos3d[v], size=vertex_size, texture=texture, **kwds)
    1470714755                if self._directed:
    1470814756                    for color in edge_colors:
    1470914757                        for u, v, l in edge_colors[color]:
    class GenericGraph(GenericGraph_pyx): 
    1472814776            if color_by_label:
    1472914777                if edge_colors is  None:
    1473014778                    # do the coloring
    14731                     edge_colors = self._color_by_label(format='rgbtuple')
     14779                    edge_colors = self._color_by_label(format=color_by_label)
    1473214780
    1473314781            if edge_colors is None:
    1473414782                edge_colors = { (0,0,0) : edges }
    class GenericGraph(GenericGraph_pyx): 
    1488914937          are colors and values are list of edges. The list of edges need not to
    1489014938          be complete in which case the default color is used.
    1489114939
    14892         - ``color_by_label`` - boolean (default: False): whether to
    14893           color each edge with a different color according to its
    14894           label. This overwrites the options ``edge_color`` and ``edge_colors``.
     14940        - ``color_by_label`` - a boolean or dictionary or function (default: False)
     14941           whether to color each edge with a different color according
     14942           to its label; the colors are chosen along a rainbow, unless
     14943           they are specified by a function or dictionary mapping
     14944           labels to colors; this option is incompatible with
     14945           ``edge_color`` and ``edge_colors``.
    1489514946
    1489614947        - ``edge_options`` - a function (or tuple thereof) mapping
    1489714948          edges to a dictionary of options for this edge.
  • sage/graphs/graph_plot.py

    diff --git a/sage/graphs/graph_plot.py b/sage/graphs/graph_plot.py
    a b graphplot_options.update( 
    6262                    'edge_style': 'The linestyle of the edges-- one of "solid", "dashed", "dotted", dashdot". This currently only works for directed graphs, since we pass off the undirected graph to networkx',
    6363                    'edge_color': 'The default color for edges.',
    6464                    'edge_colors': 'a dictionary specifying edge colors: each key is a color recognized by matplotlib, and each entry is a list of edges.',
    65                     'color_by_label': 'Whether or not to color the edges by their label values.',
     65                    'color_by_label': 'Whether to color the edges according to their labels. This also accepts a function or dictionary mapping labels to colors.',
    6666                    'partition': 'A partition of the vertex set.  If specified, plot will show each cell in a different color. vertex_colors takes precedence.',
    6767                    'loop_size': 'The radius of the smallest loop.',
    6868                    'dist': 'The distance between multiedges.',
    class GraphPlot(SageObject): 
    501501        # Make dict collection of all edges (keep label and edge color)
    502502        edges_to_draw = {}
    503503        if self._options['color_by_label'] or isinstance(self._options['edge_colors'], dict):
    504             if self._options['color_by_label']: edge_colors = self._graph._color_by_label()
     504            if self._options['color_by_label']:
     505                edge_colors = self._graph._color_by_label(format=self._options['color_by_label'])
    505506            else: edge_colors = self._options['edge_colors']
    506507            for color in edge_colors:
    507508                for edge in edge_colors[color]: