Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor details in graph_plot #36162

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions src/sage/graphs/graph_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@
'Which graphviz layout program to use -- one of '
'"circo", "dot", "fdp", "neato", or "twopi".',
'by_component':
'Whether to do the spring layout by connected component -- a boolean.',
}
'Whether to do the spring layout by connected component -- a boolean.'}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is an improvement. Is there a standard regarding this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fixing a pep8 warning issued by pycodestyle, but not one already activated in our linter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then. Thanks.

graphplot_options = layout_options.copy()

Expand Down Expand Up @@ -225,8 +224,7 @@
'graph_border':
'Whether or not to draw a frame around the graph.',
'edge_labels_background':
'The color of the background of the edge labels.',
})
'The color of the background of the edge labels.'})

_PLOT_OPTIONS_TABLE = ""

Expand All @@ -235,9 +233,7 @@

__doc__ = __doc__.format(PLOT_OPTIONS_TABLE=_PLOT_OPTIONS_TABLE)

DEFAULT_SHOW_OPTIONS = {
'figsize' : (4, 4)
}
DEFAULT_SHOW_OPTIONS = {'figsize': (4, 4)}

DEFAULT_PLOT_OPTIONS = {
'vertex_size' : 200,
Expand All @@ -258,8 +254,7 @@
'dist' : .075,
'max_dist' : 1.5,
'loop_size' : .075,
'edge_labels_background' : 'white'
}
'edge_labels_background' : 'white'}


class GraphPlot(SageObject):
Expand Down Expand Up @@ -582,7 +577,7 @@ def set_edges(self, **edge_options):
Set edge plotting parameters for the ``GraphPlot`` object.
This function is called by the constructor but can also be called to
update the vertex options of an existing ``GraphPlot`` object.
update the edge options of an existing ``GraphPlot`` object.
Note that the changes are cumulative.
EXAMPLES::
Expand Down Expand Up @@ -719,8 +714,8 @@ def set_edges(self, **edge_options):
if 'edge_style' in self._options:
from sage.plot.misc import get_matplotlib_linestyle
eoptions['linestyle'] = get_matplotlib_linestyle(
self._options['edge_style'],
return_type='long')
self._options['edge_style'],
return_type='long')
if 'edge_thickness' in self._options:
eoptions['thickness'] = self._options['edge_thickness']

Expand Down Expand Up @@ -832,20 +827,20 @@ def set_edges(self, **edge_options):
# Compute perpendicular bisector
p1 = self._pos[a]
p2 = self._pos[b]
m = ((p1[0] + p2[0])/2., (p1[1] + p2[1])/2.) # midpoint
m = ((p1[0] + p2[0]) / 2., (p1[1] + p2[1]) / 2.) # midpoint
if not p1[1] == p2[1]:
s = (p1[0] - p2[0])/(p2[1] - p1[1]) # perp slope
s = (p1[0] - p2[0]) / (p2[1] - p1[1]) # perp slope

def y(x):
return s*(x - m[0]) + m[1] # perp bisector line
return s * (x - m[0]) + m[1] # perp bisector line

# f, g are functions to determine x-values of point
# on line y at distance d from point m (on each side)
def f(d):
return sqrt(d**2/(1. + s**2)) + m[0]
return sqrt(d**2 / (1. + s**2)) + m[0]

def g(d):
return -sqrt(d**2/(1. + s**2)) + m[0]
return -sqrt(d**2 / (1. + s**2)) + m[0]

odd_x = f
even_x = g
Expand Down Expand Up @@ -897,31 +892,31 @@ def even_xy(d):
even_end = ph(even_xy(k), p2, vr)[1]
self._plot_components['edges'].append(
arrow(path=[[odd_start, odd_xy(k), odd_end]],
head=local_labels[2*i][2], zorder=1,
rgbcolor=local_labels[2*i][1],
head=local_labels[2 * i][2], zorder=1,
rgbcolor=local_labels[2 * i][1],
**eoptions))
self._plot_components['edges'].append(
arrow(path=[[even_start, even_xy(k), even_end]],
head=local_labels[2*i + 1][2], zorder=1,
rgbcolor=local_labels[2*i + 1][1],
head=local_labels[2 * i + 1][2], zorder=1,
rgbcolor=local_labels[2 * i + 1][1],
**eoptions))
else:
self._plot_components['edges'].append(
bezier_path([[p1, odd_xy(k), p2]], zorder=1,
rgbcolor=local_labels[2*i][1],
rgbcolor=local_labels[2 * i][1],
**eoptions))
self._plot_components['edges'].append(
bezier_path([[p1, even_xy(k), p2]], zorder=1,
rgbcolor=local_labels[2*i + 1][1],
rgbcolor=local_labels[2 * i + 1][1],
**eoptions))
if labels:
j = k / 2.0
bg = self._options['edge_labels_background']
self._plot_components['edge_labels'].append(
text(local_labels[2*i][0], odd_xy(j),
text(local_labels[2 * i][0], odd_xy(j),
background_color=bg))
self._plot_components['edge_labels'].append(
text(local_labels[2*i + 1][0], even_xy(j),
text(local_labels[2 * i + 1][0], even_xy(j),
background_color=bg))
if len_local_labels % 2:
# draw line for last odd
Expand All @@ -941,7 +936,7 @@ def even_xy(d):
bg = self._options['edge_labels_background']
self._plot_components['edge_labels'].append(
text(str(edges_to_draw[a, b][0][0]),
[(C[0] + D[0])/2., (C[1] + D[1])/2.],
[(C[0] + D[0]) / 2., (C[1] + D[1]) / 2.],
background_color=bg))
elif is_directed:
self._plot_components['edges'].append(
Expand All @@ -959,8 +954,8 @@ def even_xy(d):
bg = self._options['edge_labels_background']
self._plot_components['edge_labels'].append(
text(str(edges_to_draw[a, b][0][0]),
[(self._pos[a][0] + self._pos[b][0])/2.,
(self._pos[a][1] + self._pos[b][1])/2.],
[(self._pos[a][0] + self._pos[b][0]) / 2.,
(self._pos[a][1] + self._pos[b][1]) / 2.],
background_color=bg))

def _polar_hack_for_multidigraph(self, A, B, VR):
Expand Down Expand Up @@ -991,7 +986,6 @@ def _polar_hack_for_multidigraph(self, A, B, VR):
sage: GP._polar_hack_for_multidigraph((int(0), int(1)),
....: (int(2), int(2)), .1)
([0.08..., 1.04...], [1.91..., 1.95...])
"""
D = [float(B[i] - A[i]) for i in range(2)]
R = sqrt(D[0]**2 + D[1]**2)
Expand Down Expand Up @@ -1415,7 +1409,7 @@ def plot(self, **kwds):
sage: p = graphs.PetersenGraph().plot(egabrag='garbage')
Traceback (most recent call last):
...
ValueError: Invalid input 'egabrag=garbage'
ValueError: invalid input 'egabrag=garbage'
Make sure that no graphics primitive is clipped::
Expand All @@ -1441,7 +1435,7 @@ def plot(self, **kwds):
# Check the arguments
for o in options:
if o not in graphplot_options and o not in G._extra_kwds:
raise ValueError("Invalid input '{}={}'".format(o, options[o]))
raise ValueError("invalid input '{}={}'".format(o, options[o]))

for comp in self._plot_components.values():
if not isinstance(comp, list):
Expand Down Expand Up @@ -1488,13 +1482,12 @@ def layout_tree(self, root, orientation):
sage: T = Graph()
sage: T.add_edges(G.min_spanning_tree(starting_vertex=0))
sage: T.show(layout='tree', tree_root=0) # indirect doctest
"""
T = self._graph

if not self._graph.is_tree():
raise RuntimeError("Cannot use tree layout on this graph: "
"self.is_tree() returns False.")
raise RuntimeError("cannot use tree layout on this graph: "
"self.is_tree() returns False")

children = {root: T.neighbors(root)}

Expand Down Expand Up @@ -1544,7 +1537,7 @@ def slide(v, dx):
if x < ox:
slide(p, ox - x)
x = ox
obstruction[y] = x+1
obstruction[y] = x + 1
continue

t = C.pop()
Expand Down
Loading