Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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?

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.

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.

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?

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.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py 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.

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?

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.S. I tried g=DiGraph() g.plot? and was directed to a file decorators.py 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

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.

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

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