ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 03 Jan 2015 22:12:48 -0600Can I draw a graph whose vertices have two kind of labels?http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/ Hello.
I want to draw a graph.
Each vertex connects to another information.
For example,
G=graphs.EmptyGraph()
G.add_vertices([1,2,3,4])
for i in [1,2,3]:
for j in [i+1..4]:
if j%i==0:
G.add_edge([i,j])
V=[]
for i in G.vertices():
if i<3:
V=V+["Dog"]
else:
V=V+["Cat"]
Then 1,2 --> Dog and 3,4 --> Cat.
**I want to get a plotting graph which shows the second information Dog and Cat.**
Is that possible?
Thanks.Fri, 02 Jan 2015 07:14:14 -0600http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/Answer by tmonteil for <p>Hello.</p>
<p>I want to draw a graph.</p>
<p>Each vertex connects to another information.</p>
<p>For example,</p>
<pre><code>G=graphs.EmptyGraph()
G.add_vertices([1,2,3,4])
for i in [1,2,3]:
for j in [i+1..4]:
if j%i==0:
G.add_edge([i,j])
V=[]
for i in G.vertices():
if i<3:
V=V+["Dog"]
else:
V=V+["Cat"]
</code></pre>
<p>Then 1,2 --> Dog and 3,4 --> Cat. </p>
<p><strong>I want to get a plotting graph which shows the second information Dog and Cat.</strong></p>
<p>Is that possible?</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/?answer=25381#post-id-25381Unfortunately, there is no way to define non-injective labeling for vertices of Sage graphs as there is a confusion between the vertices themselves and possible labels on them, see [the documentation](http://sagemath.org/doc/reference/graphs/sage/graphs/graph.html#labels).
For example:
sage: G.relabel(V)
NotImplementedError: Non injective relabeling
There are two possible workarounds. You can make the pseudo-labelling injective as follows:
sage: G.relabel(list(enumerate(V)))
sage: G.vertices()
[(0, 'Dog'), (1, 'Dog'), (2, 'Cat'), (3, 'Cat')]
sage: G.plot()
You can also use colors during the plot to pseudo-label the vertices:
sage: G.plot(partition=[[1,2],[3,4]])
sage: G.plot(partition=[[1,2],[3,4]], vertex_labels=False)
Sat, 03 Jan 2015 01:57:32 -0600http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/?answer=25381#post-id-25381Comment by Semin for <p>Unfortunately, there is no way to define non-injective labeling for vertices of Sage graphs as there is a confusion between the vertices themselves and possible labels on them, see <a href="http://sagemath.org/doc/reference/graphs/sage/graphs/graph.html#labels">the documentation</a>.</p>
<p>For example:</p>
<pre><code>sage: G.relabel(V)
NotImplementedError: Non injective relabeling
</code></pre>
<p>There are two possible workarounds. You can make the pseudo-labelling injective as follows:</p>
<pre><code>sage: G.relabel(list(enumerate(V)))
sage: G.vertices()
[(0, 'Dog'), (1, 'Dog'), (2, 'Cat'), (3, 'Cat')]
sage: G.plot()
</code></pre>
<p>You can also use colors during the plot to pseudo-label the vertices:</p>
<pre><code>sage: G.plot(partition=[[1,2],[3,4]])
sage: G.plot(partition=[[1,2],[3,4]], vertex_labels=False)
</code></pre>
http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/?comment=25393#post-id-25393Thanks for nice advise~ That is very helpful for me. :)Sat, 03 Jan 2015 22:12:48 -0600http://ask.sagemath.org/question/25377/can-i-draw-a-graph-whose-vertices-have-two-kind-of-labels/?comment=25393#post-id-25393