ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 01 Apr 2014 11:18:32 +0200graph vertex labels placement or alignmenthttps://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/I made a sage Graph() for visualizing a bibliography and found that there was no good way to align or change the placement of the vertex labels.
I wanted some labels to align left and others to align right, but they were all centred on their vertex. I ended up doubling the length of the label string with spaces and adding a period with some code like the following
lDict = {}
for v in G.vertices():
if <test for the type of vertex>:
lDict[v] = v + len(v)*' ' + '.'
else:
lDict[v] = ''.join(['.',len(v)*' ',v])
G.relabel(lDict)
I managed to get this (click for full sized image):
[![Graph](http://alejandroerickson.com/home/blog/im/small_bibgraph.jpg)](http://alejandroerickson.com/home/blog/im/full_sized_bibgraph.png)
Is there a way to manipulate the placement of the labels?Mon, 31 Mar 2014 09:40:01 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/Answer by Nathann for <p>I made a sage Graph() for visualizing a bibliography and found that there was no good way to align or change the placement of the vertex labels.</p>
<p>I wanted some labels to align left and others to align right, but they were all centred on their vertex. I ended up doubling the length of the label string with spaces and adding a period with some code like the following</p>
<pre><code> lDict = {}
for v in G.vertices():
if <test for the type of vertex>:
lDict[v] = v + len(v)*' ' + '.'
else:
lDict[v] = ''.join(['.',len(v)*' ',v])
G.relabel(lDict)
</code></pre>
<p>I managed to get this (click for full sized image):</p>
<p><a href="http://alejandroerickson.com/home/blog/im/full_sized_bibgraph.png"><img alt="Graph" src="http://alejandroerickson.com/home/blog/im/small_bibgraph.jpg"/></a></p>
<p>Is there a way to manipulate the placement of the labels?</p>
https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?answer=16108#post-id-16108No way that I know. But Sage is an open-source project, so if you have a good idea for that, you are welcome to contribute !
NathannMon, 31 Mar 2014 10:24:44 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?answer=16108#post-id-16108Answer by kcrisman for <p>I made a sage Graph() for visualizing a bibliography and found that there was no good way to align or change the placement of the vertex labels.</p>
<p>I wanted some labels to align left and others to align right, but they were all centred on their vertex. I ended up doubling the length of the label string with spaces and adding a period with some code like the following</p>
<pre><code> lDict = {}
for v in G.vertices():
if <test for the type of vertex>:
lDict[v] = v + len(v)*' ' + '.'
else:
lDict[v] = ''.join(['.',len(v)*' ',v])
G.relabel(lDict)
</code></pre>
<p>I managed to get this (click for full sized image):</p>
<p><a href="http://alejandroerickson.com/home/blog/im/full_sized_bibgraph.png"><img alt="Graph" src="http://alejandroerickson.com/home/blog/im/small_bibgraph.jpg"/></a></p>
<p>Is there a way to manipulate the placement of the labels?</p>
https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?answer=16109#post-id-16109I believe you'll want to use the optional tikz interface, lovely described (complete with vertex label positioning) at [this location](http://www.sagemath.org/doc/reference/graphs/sage/graphs/graph_latex.html#sage.graphs.graph_latex.GraphLatex.set_option).Mon, 31 Mar 2014 12:03:34 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?answer=16109#post-id-16109Comment by kcrisman for <p>I believe you'll want to use the optional tikz interface, lovely described (complete with vertex label positioning) at <a href="http://www.sagemath.org/doc/reference/graphs/sage/graphs/graph_latex.html#sage.graphs.graph_latex.GraphLatex.set_option">this location</a>.</p>
https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16209#post-id-16209Great! I have to admit tikz is pretty awesome - the learning curve is worth it. If this solved your problem, please accept the answer so other people searching this question will see it was solved.Tue, 01 Apr 2014 10:23:37 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16209#post-id-16209Comment by alejandroerickson for <p>I believe you'll want to use the optional tikz interface, lovely described (complete with vertex label positioning) at <a href="http://www.sagemath.org/doc/reference/graphs/sage/graphs/graph_latex.html#sage.graphs.graph_latex.GraphLatex.set_option">this location</a>.</p>
https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16208#post-id-16208As of now it doesn't work on my installation, but I'll select it when it works for me. Thanks!Tue, 01 Apr 2014 11:18:32 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16208#post-id-16208Comment by alejandroerickson for <p>I believe you'll want to use the optional tikz interface, lovely described (complete with vertex label positioning) at <a href="http://www.sagemath.org/doc/reference/graphs/sage/graphs/graph_latex.html#sage.graphs.graph_latex.GraphLatex.set_option">this location</a>.</p>
https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16210#post-id-16210The example is pretty. I think I might use the tikz graphs every time!Mon, 31 Mar 2014 12:34:51 +0200https://ask.sagemath.org/question/11349/graph-vertex-labels-placement-or-alignment/?comment=16210#post-id-16210