pepsi-maniac
pepsi-maniac

Reputation: 105

SageMath: saving picture of graph to a file

Given a graph defined in SageMath:

G = Graph({...})

one can call G.show() to open a preview. But how can I save this picture to file instead?

I'm aware I can do this within the preview dialog, but I can't see an option to do this from code.

Something like .write('/tmp/file').

I can use latex, write the output to a file, and then compile it with LaTeX, but this seems a little round the corner:

Upvotes: 2

Views: 1034

Answers (1)

davidlowryduda
davidlowryduda

Reputation: 2559

The result of a call to graph.plot() is a sage Graphics object, which is the fundamental object for manipulating and saving graphics. To save the plot, name the Graphics object and call one of its save methods.

For a complete minimum example:

# In sage session or script
G = graphs.WheelGraph(15)
p = G.plot()
p.save_image("myimage.png")  # saves the graph to myimage.png

Upvotes: 3

Related Questions