# 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.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.py

edit retag close merge delete

Last 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.

It 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.

Which graph plotting procedures are you referring to?

Sort by » oldest newest most voted

It 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 could be useful.

more

I 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.

I am aware of list comprehension. I don't see how this helps.

Maybe @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.

I was asked to produce a toy example. Maybe I did not choose a good example.

I 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.