'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:



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 davidlowryduda