Skip to content

Commit

Permalink
Trac #13521: Make vertex labels more flexible
Browse files Browse the repository at this point in the history
Over lunch today, Jamie Radcliffe at University of Nebraska, Lincoln,
suggested making the vertex label plotting more flexible to take a
dictionary or function that gives the vertex labels.  This patch makes
that possible.

Still to do: do the same thing for the edge_labels parameter.

URL: https://trac.sagemath.org/13521
Reported by: jason
Ticket author(s): Jason Grout, David Coudert
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Oct 10, 2021
2 parents 244698d + 1bbe46b commit 5cf2b39
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions src/sage/graphs/graph_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@
# ****************************************************************************

from collections import defaultdict
from math import sqrt, cos, sin, acos, atan, pi
from math import sqrt, cos, sin, atan, pi
from sage.structure.sage_object import SageObject
from sage.plot.all import (
Graphics, scatter_plot, bezier_path, line, arrow, text, arc, circle)
Graphics, scatter_plot, bezier_path, line, arrow, text, circle)

layout_options = {
'layout':
Expand Down Expand Up @@ -175,7 +175,11 @@
'pos':
'The position dictionary of vertices.',
'vertex_labels':
'Whether or not to draw vertex labels.',
'Vertex labels to draw. This can be ``True``/``False`` to indicate '
'whether to print the vertex string representation of not, '
'a dictionary keyed by vertices and associating to each vertex '
'a label string, or a function taking as input a vertex and returning '
'a label string.',
'vertex_color':
'Default color for vertices not listed '
'in vertex_colors dictionary.',
Expand Down Expand Up @@ -429,6 +433,54 @@ def set_vertices(self, **vertex_options):
GP.set_vertices(talk=True)
GP.set_vertices(vertex_color='green', vertex_shape='^')
sphinx_plot(GP)
Vertex labels are flexible::
sage: g = graphs.PathGraph(4)
sage: g.plot(vertex_labels=False)
Graphics object consisting of 4 graphics primitives
.. PLOT::
g = graphs.PathGraph(4)
P = g.graphplot(vertex_labels=False)
sphinx_plot(P)
::
sage: g = graphs.PathGraph(4)
sage: g.plot(vertex_labels=True)
Graphics object consisting of 8 graphics primitives
.. PLOT::
g = graphs.PathGraph(4)
P = g.graphplot(vertex_labels=True)
sphinx_plot(P)
::
sage: g = graphs.PathGraph(4)
sage: g.plot(vertex_labels=dict(zip(g, ['+', '-', '/', '*'])))
Graphics object consisting of 8 graphics primitives
.. PLOT::
g = graphs.PathGraph(4)
P = g.graphplot(vertex_labels=dict(zip(g, ['+', '-', '/', '*'])))
sphinx_plot(P)
::
sage: g = graphs.PathGraph(4)
sage: g.plot(vertex_labels=lambda x: str(x % 2))
Graphics object consisting of 8 graphics primitives
.. PLOT::
g = graphs.PathGraph(4)
P = g.graphplot(vertex_labels=lambda x: str(x % 2))
sphinx_plot(P)
"""
# Handle base vertex options
voptions = {}
Expand Down Expand Up @@ -509,12 +561,18 @@ def set_vertices(self, **vertex_options):
self._plot_components['vertices'] = scatter_plot(
pos, facecolor=colors, clip=False, **voptions)

if self._options['vertex_labels']:
self._plot_components['vertex_labels'] = []
vlabels = self._options['vertex_labels']
if vlabels:
if vlabels is True:
vfun = str
elif isinstance(vlabels, dict):
def vfun(x):
return vlabels.get(x, "")
else:
vfun = vlabels
# TODO: allow text options
for v in self._nodelist:
self._plot_components['vertex_labels'].append(
text(str(v), self._pos[v], rgbcolor=(0, 0, 0), zorder=8))
self._plot_components['vertex_labels'] = [text(vfun(v), self._pos[v], color='black', zorder=8)
for v in self._nodelist]

def set_edges(self, **edge_options):
"""
Expand Down

0 comments on commit 5cf2b39

Please sign in to comment.