Ask Your Question
1

Working with sage graphics

asked 2011-06-28 08:02:28 +0100

BWW gravatar image

updated 2011-06-29 07:40:01 +0100

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

edit retag flag offensive close merge delete

Comments

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.

kcrisman gravatar imagekcrisman ( 2011-06-28 10:16:48 +0100 )edit

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.

kcrisman gravatar imagekcrisman ( 2011-06-28 10:17:58 +0100 )edit

Which graph plotting procedures are you referring to?

BWW gravatar imageBWW ( 2011-06-29 05:53:41 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2011-06-29 11:03:33 +0100

kcrisman gravatar image

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.

edit flag offensive delete link more

Comments

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.

BWW gravatar imageBWW ( 2011-06-29 11:41:32 +0100 )edit

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

BWW gravatar imageBWW ( 2011-06-29 11:44:08 +0100 )edit

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.

benjaminfjones gravatar imagebenjaminfjones ( 2011-06-29 13:04:36 +0100 )edit

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

BWW gravatar imageBWW ( 2011-06-29 14:07:06 +0100 )edit

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.

kcrisman gravatar imagekcrisman ( 2011-06-29 14:48:16 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-06-28 08:02:28 +0100

Seen: 788 times

Last updated: Jun 29 '11