Ask Your Question
0

How can I save an image from sage plot ?

asked 2013-04-08 09:36:12 +0100

Chitrank Dixit gravatar image

I am working on a project that needs the graph image generated by sage to save to disk then that would be retrieved in the application. Here is the code I am writing for generating a Graph

G=Graph({'a': {'c': 25, 'b': 26, 'e': 30, 'd': 35}, 'c': {'a': 25, 'b': 33, 'e':
27, 'd': 7}, 'b': {'a': 26, 'c': 33, 'e': 34, 'd': 7}, 'e': {'a': 30,
'c': 27, 'b': 34, 'd': 9}, 'd': {'a': 35, 'c': 7, 'b': 7, 'e': 9}})

G.weighted(True)

E = kruskal(G, check=True)

print E

tot=[]

kcost=0;totcost=0

for key in E:

        tot.append((key[0],key[1]))
        kcost=kcost+key[2]

rev=tot[::-1]

for key in rev:

        tot.append((key[1],key[0]))

path=[]

tot1=[]    

for key in E:

        tot1.append((key[0],key[1]))
        tot1.append((key[0],key[1]))
        kcost=kcost+key[2]


for p in tot1:

        if p[0] not in path:
            path.append(p[0])
        if p[1] not in path:
            path.append(p[1])

print path 

print tot1    

kcost=totcost/2

print tot,totcost,kcost

N=Graph(E)

N.show()

N1=Graph(tot1)

N1.show()

N.show() and N1.show() will show the Graph describing Now I am facing error running the file and generating image and my image viewer gives me error as in this post of your Google + community I have posted

https://plus.google.com/u/0/113942220...

So I am Planning to save the image at my selected location and fetch the image from that Position. for that I am using in my code and unsure where to supply a or how to save it Please suggest me how should I save the Images from show() method. I am using Python2.7 does it require some additional package of sagemath.

N1=Graph(tot)
a=N1.show()
newimage=Image.new("RGB",(500,500),"white")
newimage.load()    
newimage.save("out.png")
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2013-04-08 10:47:14 +0100

fidbc gravatar image

You will need to use the plot method to be able to save, as the show method returns None. Say G is a Graph. Then the following saves a picture of G to filename.png.

sage: p = G.plot()
sage: p.save('filename.png')
edit flag offensive delete link more
2

answered 2013-04-08 10:55:23 +0100

Volker Braun gravatar image

The show() method calls plot(), saves to a temporary file, and then runs an image viewer with the output. If you just want the file, use plot().save('filename.png').

edit flag offensive delete link more
0

answered 2013-10-30 03:37:52 +0100

updated 2013-10-30 07:29:11 +0100

tmonteil gravatar image

spam removed

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-04-08 09:36:12 +0100

Seen: 12,990 times

Last updated: Oct 30 '13