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.Mon, 04 Jul 2011 13:45:49 -0500Working with sage graphicshttp://ask.sagemath.org/question/8195/working-with-sage-graphics/I have written some code to plot some graphs (that is vertices and edges) and it has been suggested that I am being inefficient. I have say 50-100 primitives (consisting of lines and circles). At the moment I use to Graphics() to create an empty graphics object and then use + to add the primitives. Is there a better way to do this?
**Edit** The add_primitive() is one improvement. Here is a toy example
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g += line([(i,j),(i+1,j)])
g += line([(i,j),(i,j+1)])
g.show()
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g.add_primitive(line([(i,j),(i+1,j)]))
g.add_primitive(line([(i,j),(i,j+1)]))
g.show()
A second question is that I would also like to plot a formal linear combination of these graphs. At the moment I produce a list with a graph and the coefficients (via latex) alternating and then use graphics_array(). However I would prefer to group the primitives in a graph and in the latex and then scale these and place them myself.
**Edit** This is the code for my second question. The variable a is a formal linear sum (using CombinatorialFreeModule). The function circlepacking takes a graph and produces a graphics object.
r = a.monomial_coefficients()
pics = []
for f in r:
c = r[f]
if c == 1: lt = "$+$"
elif c == -1: lt = "$-$"
else: lt = "$+("+latex(c)+")$"
pics.append(text(lt,(0,0)))
pics.append(circlepacking(f,bv))
graphics_array(pics).show(axes=False,aspect_ratio=1)
I have looked at the matplotlib home page. I can see that there is a class Figure and so on. Should I be attempting to use these since I am working in sage?
I have used metapost and tikz in the past but not matlab.
P.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.pyTue, 28 Jun 2011 01:02:28 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/Comment by kcrisman for <p>I have written some code to plot some graphs (that is vertices and edges) and it has been suggested that I am being inefficient. I have say 50-100 primitives (consisting of lines and circles). At the moment I use to Graphics() to create an empty graphics object and then use + to add the primitives. Is there a better way to do this?</p>
<p><strong>Edit</strong> The add_primitive() is one improvement. Here is a toy example</p>
<pre><code>n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g += line([(i,j),(i+1,j)])
g += line([(i,j),(i,j+1)])
g.show()
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g.add_primitive(line([(i,j),(i+1,j)]))
g.add_primitive(line([(i,j),(i,j+1)]))
g.show()
</code></pre>
<p>A second question is that I would also like to plot a formal linear combination of these graphs. At the moment I produce a list with a graph and the coefficients (via latex) alternating and then use graphics_array(). However I would prefer to group the primitives in a graph and in the latex and then scale these and place them myself.</p>
<p><strong>Edit</strong> This is the code for my second question. The variable a is a formal linear sum (using CombinatorialFreeModule). The function circlepacking takes a graph and produces a graphics object.</p>
<pre><code>r = a.monomial_coefficients()
pics = []
for f in r:
c = r[f]
if c == 1: lt = "$+$"
elif c == -1: lt = "$-$"
else: lt = "$+("+latex(c)+")$"
pics.append(text(lt,(0,0)))
pics.append(circlepacking(f,bv))
graphics_array(pics).show(axes=False,aspect_ratio=1)
</code></pre>
<p>I have looked at the matplotlib home page. I can see that there is a class Figure and so on. Should I be attempting to use these since I am working in sage?</p>
<p>I have used metapost and tikz in the past but not matlab.</p>
<p>P.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21540#post-id-21540Last thing first: the entry for g.plot? is still correct, right? There is a pretty long entry there. There is probably a default input decorator involved which accounts for the incorrect file.Tue, 28 Jun 2011 03:16:48 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21540#post-id-21540Comment by BWW for <p>I have written some code to plot some graphs (that is vertices and edges) and it has been suggested that I am being inefficient. I have say 50-100 primitives (consisting of lines and circles). At the moment I use to Graphics() to create an empty graphics object and then use + to add the primitives. Is there a better way to do this?</p>
<p><strong>Edit</strong> The add_primitive() is one improvement. Here is a toy example</p>
<pre><code>n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g += line([(i,j),(i+1,j)])
g += line([(i,j),(i,j+1)])
g.show()
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g.add_primitive(line([(i,j),(i+1,j)]))
g.add_primitive(line([(i,j),(i,j+1)]))
g.show()
</code></pre>
<p>A second question is that I would also like to plot a formal linear combination of these graphs. At the moment I produce a list with a graph and the coefficients (via latex) alternating and then use graphics_array(). However I would prefer to group the primitives in a graph and in the latex and then scale these and place them myself.</p>
<p><strong>Edit</strong> This is the code for my second question. The variable a is a formal linear sum (using CombinatorialFreeModule). The function circlepacking takes a graph and produces a graphics object.</p>
<pre><code>r = a.monomial_coefficients()
pics = []
for f in r:
c = r[f]
if c == 1: lt = "$+$"
elif c == -1: lt = "$-$"
else: lt = "$+("+latex(c)+")$"
pics.append(text(lt,(0,0)))
pics.append(circlepacking(f,bv))
graphics_array(pics).show(axes=False,aspect_ratio=1)
</code></pre>
<p>I have looked at the matplotlib home page. I can see that there is a class Figure and so on. Should I be attempting to use these since I am working in sage?</p>
<p>I have used metapost and tikz in the past but not matlab.</p>
<p>P.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21535#post-id-21535Which graph plotting procedures are you referring to?Tue, 28 Jun 2011 22:53:41 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21535#post-id-21535Comment by kcrisman for <p>I have written some code to plot some graphs (that is vertices and edges) and it has been suggested that I am being inefficient. I have say 50-100 primitives (consisting of lines and circles). At the moment I use to Graphics() to create an empty graphics object and then use + to add the primitives. Is there a better way to do this?</p>
<p><strong>Edit</strong> The add_primitive() is one improvement. Here is a toy example</p>
<pre><code>n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g += line([(i,j),(i+1,j)])
g += line([(i,j),(i,j+1)])
g.show()
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g.add_primitive(line([(i,j),(i+1,j)]))
g.add_primitive(line([(i,j),(i,j+1)]))
g.show()
</code></pre>
<p>A second question is that I would also like to plot a formal linear combination of these graphs. At the moment I produce a list with a graph and the coefficients (via latex) alternating and then use graphics_array(). However I would prefer to group the primitives in a graph and in the latex and then scale these and place them myself.</p>
<p><strong>Edit</strong> This is the code for my second question. The variable a is a formal linear sum (using CombinatorialFreeModule). The function circlepacking takes a graph and produces a graphics object.</p>
<pre><code>r = a.monomial_coefficients()
pics = []
for f in r:
c = r[f]
if c == 1: lt = "$+$"
elif c == -1: lt = "$-$"
else: lt = "$+("+latex(c)+")$"
pics.append(text(lt,(0,0)))
pics.append(circlepacking(f,bv))
graphics_array(pics).show(axes=False,aspect_ratio=1)
</code></pre>
<p>I have looked at the matplotlib home page. I can see that there is a class Figure and so on. Should I be attempting to use these since I am working in sage?</p>
<p>I have used metapost and tikz in the past but not matlab.</p>
<p>P.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21539#post-id-21539It would be really helpful to see some (toy) examples of what you are saying; that would make it easier to see whether you could improve it. But it certainly sounds like you should use the graph plotting procedures, which allow a lot of customization of placement of vertices and so forth. Tue, 28 Jun 2011 03:17:58 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21539#post-id-21539Answer by kcrisman for <p>I have written some code to plot some graphs (that is vertices and edges) and it has been suggested that I am being inefficient. I have say 50-100 primitives (consisting of lines and circles). At the moment I use to Graphics() to create an empty graphics object and then use + to add the primitives. Is there a better way to do this?</p>
<p><strong>Edit</strong> The add_primitive() is one improvement. Here is a toy example</p>
<pre><code>n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g += line([(i,j),(i+1,j)])
g += line([(i,j),(i,j+1)])
g.show()
n = 10
g = Graphics()
for i in range(n):
for j in range(n):
g.add_primitive(line([(i,j),(i+1,j)]))
g.add_primitive(line([(i,j),(i,j+1)]))
g.show()
</code></pre>
<p>A second question is that I would also like to plot a formal linear combination of these graphs. At the moment I produce a list with a graph and the coefficients (via latex) alternating and then use graphics_array(). However I would prefer to group the primitives in a graph and in the latex and then scale these and place them myself.</p>
<p><strong>Edit</strong> This is the code for my second question. The variable a is a formal linear sum (using CombinatorialFreeModule). The function circlepacking takes a graph and produces a graphics object.</p>
<pre><code>r = a.monomial_coefficients()
pics = []
for f in r:
c = r[f]
if c == 1: lt = "$+$"
elif c == -1: lt = "$-$"
else: lt = "$+("+latex(c)+")$"
pics.append(text(lt,(0,0)))
pics.append(circlepacking(f,bv))
graphics_array(pics).show(axes=False,aspect_ratio=1)
</code></pre>
<p>I have looked at the matplotlib home page. I can see that there is a class Figure and so on. Should I be attempting to use these since I am working in sage?</p>
<p>I have used metapost and tikz in the past but not matlab.</p>
<p>P.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?answer=12475#post-id-12475It sounds like you may find
sage: Graph?
very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).
Once you have created your graph (or DiGraph) you can then plot it.
sage: G = graphs.PetersenGraph()
sage: G.plot?
will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even *better* plot options, if you have LaTeX and tikz, you can look at
sage: sage.graphs.graph_latex.GraphLatex.set_option?
which has more information than I really want, because I don't plot graphs that often.
Hope this helps... I'm not answering the part about adding objects, note, but a [list comprehension](http://diveintopython.org/native_data_types/mapping_lists.html) could be useful.Wed, 29 Jun 2011 04:03:33 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?answer=12475#post-id-12475Comment by Jason Grout for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21506#post-id-21506Look at graph_plot.py in the graphs directory. We also plot planar layouts of graphs, and for that I think we keep track of the order of edges around the vertex (so that we can calculate faces, etc.)Mon, 04 Jul 2011 13:45:49 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21506#post-id-21506Comment by kcrisman for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21529#post-id-21529I see what's going on now, because in ribbon graphs the order around the vertex counts. But your question was very misleading, since a ribbon graph isn't a graph in Sage's sense. What I recommend is to extend the graph class (or write your own) to represent the mathematical structure first, preserving order of edges around a vertex etc., and only then trying to plot it - where you could probably still use the Sage graph plotting algorithms as a starting point. I do think that this will be easier in Sage than matplotlib, because of the "mathematics-oriented" syntax of our plot objects.Wed, 29 Jun 2011 07:48:16 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21529#post-id-21529Comment by BWW for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21533#post-id-21533I am aware of list comprehension. I don't see how this helps.Wed, 29 Jun 2011 04:44:08 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21533#post-id-21533Comment by BWW for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21530#post-id-21530I was asked to produce a toy example. Maybe I did not choose a good example.Wed, 29 Jun 2011 07:07:06 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21530#post-id-21530Comment by BWW for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21528#post-id-21528I apologise for misleading you. I have done what you suggest. I have also written the code for plotting ribbon graphs. It works. For an individual ribbon graph it is slow. For a linear combination graphics_array works but is not really satisfactory. I have not tracked down the code for graph plotting.Wed, 29 Jun 2011 08:36:52 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21528#post-id-21528Comment by BWW for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21534#post-id-21534I am not explaining myself very well. I am implementing the mathematical notion of a ribbon graph. There is some overlap in the functionality with abstract graphs. I might learn something from the code for the sage Graph class but I can't just call the functions.Wed, 29 Jun 2011 04:41:32 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21534#post-id-21534Comment by benjaminfjones for <p>It sounds like you may find </p>
<pre><code>sage: Graph?
</code></pre>
<p>very helpful. There is ample documentation there about how to input graphs (say, as dictionaries or incidence matrices).</p>
<p>Once you have created your graph (or DiGraph) you can then plot it.</p>
<pre><code>sage: G = graphs.PetersenGraph()
sage: G.plot?
</code></pre>
<p>will give lots of information about every manner of plotting option, including setting the exact locations of each vertex. For even <em>better</em> plot options, if you have LaTeX and tikz, you can look at</p>
<pre><code>sage: sage.graphs.graph_latex.GraphLatex.set_option?
</code></pre>
<p>which has more information than I really want, because I don't plot graphs that often. </p>
<p>Hope this helps... I'm not answering the part about adding objects, note, but a <a href="http://diveintopython.org/native_data_types/mapping_lists.html">list comprehension</a> could be useful.</p>
http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21532#post-id-21532Maybe @kcrisman is saying that a list comprehension is more "pythonic" than a double nested for loop. I don't see why you "can't just call the functions" from Sage's Graph class. What you are producing in your code above is just a bunch of lines on a page with no structure behind it. If you instead define Graph objects then you have the structure of the abstract graph recorded and you can use the Graph.plot method to display that structure in whatever way you like without having to manually redraw it yourself with several paragraphs of code. Wed, 29 Jun 2011 06:04:36 -0500http://ask.sagemath.org/question/8195/working-with-sage-graphics/?comment=21532#post-id-21532