Plot 在SageMath中在图的顶点上绘制非内射标签
我有一个顶点由一些对(a,b)标记的图。 我能以这样一种方式绘制它,即我只能看到打印在每个顶点上的第一个组件“a”吗? 我不能只是重新标记,因为我的映射(a,b)->a不是内射的 举个小例子Plot 在SageMath中在图的顶点上绘制非内射标签,plot,sage,Plot,Sage,我有一个顶点由一些对(a,b)标记的图。 我能以这样一种方式绘制它,即我只能看到打印在每个顶点上的第一个组件“a”吗? 我不能只是重新标记,因为我的映射(a,b)->a不是内射的 举个小例子 G = Graph() G.add_edge((1,1),(1,2)) 通常的G.plot()。 相反,如何只生成1--1?用非内射顶点Labs绘制Sage图 我们描述了一个稍微乏味的解决方法,然后 一种恢复我们舒适的方法 顶点的新类 一种解决方案是为顶点编写一个新类 它继承自元组,并具有自定义的\uuu
G = Graph()
G.add_edge((1,1),(1,2))
通常的G.plot()。
相反,如何只生成1--1?用非内射顶点Labs绘制Sage图
我们描述了一个稍微乏味的解决方法,然后
一种恢复我们舒适的方法
顶点的新类
一种解决方案是为顶点编写一个新类
它继承自元组
,并具有自定义的\uuuu str\uuu
方法
仅返回元组中第一个项的字符串
class MyVertex(tuple):
r"""
Class for vertices for special plotting of graphs.
Use with tuples, and only the first entry in the tuple
will be used as a vertex label when plotting the graph.
"""
def __init__(self, v):
self.vertex = v
def __str__(self):
return str(self.vertex[0])
使用此选项定义图形的顶点,
我们获得了想要的行为
定义一个图形并将边从(1,1)
添加到(1,2)
:
打印图形时,两个顶点都有标签1
sage: G.plot()
Launched png viewer for Graphics object consisting of 4 graphics primitives
列出顶点时,它们仍然完全显示:
sage: G.vertices()
[(1, 1), (1, 2)]
使用常用图形
为了避免显式地使用MyVertex
,我们编写了一个图形绘图
函数,用于创建普通图形的中间“MyVertex”样式副本
为了策划
def plot_graph(G):
r"""
Return a plot of this graph with special vertex labels.
The graph vertices are assumed to be tuples. The plot
uses the first component of each tuple as a vertex label.
"""
E = [(MyVertex(a), MyVertex(b)) for (a, b) in G.edges(labels=False)]
return Graph(E).plot()
现在比较一下:
sage: G = graphs.Grid2dGraph(3, 4)
sage: G.plot()
Launched png viewer for Graphics object consisting of 30 graphics primitives
sage: plot_graph(G)
Launched png viewer for Graphics object consisting of 30 graphics primitives
sage: G = graphs.Grid2dGraph(3, 4)
sage: G.plot()
Launched png viewer for Graphics object consisting of 30 graphics primitives
sage: plot_graph(G)
Launched png viewer for Graphics object consisting of 30 graphics primitives