Ask Your Question
0

another 'how to save a plot' question

asked 2018-12-13 18:57:55 +0200

jasonemiller gravatar image

I use SageMath in CoCalc to generate images for worksheets, handouts, exams, etc. I am able to save simple plots to image files, but more complex plots like the one below vex me. Can someone tell me how to save the plot to an image file?

plot1 = plot(-(x-1)^2+3, x, -3, 2.98)
plot2 = plot(-abs(2*(x-5))+2, x, 3,5.95)
plot3 = plot((x-7)^2-1,x,6.05,10)
pt1 = circle((6, 0),.1, edgecolor='black')
pt2 = point((6, 3), rgbcolor='black', pointsize=30)
pt3 = point((1, 2), rgbcolor='black', pointsize=30)
pt4 = circle((3, -1),.1, edgecolor='black')
pt3 = point((3, -2), rgbcolor='black', pointsize=30)
g3=(plot1+plot2+plot3+pt1+pt2+pt3+pt4).show(xmin=-2, xmax=10, ymin=-4, ymax=6,gridlines="minor").save('diagram2-finalexam01.png')
g3.save('diagram2-exam02.png')

The last line throws the following error and no image file is saved:

Error in lines 9-9
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
    flags=compile_flags) in namespace, locals
  File "", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'save

I've resorted to just taking a screenshot of the plot and using it, but I'd much rather do this in a pythonistic way. Can someone point me in the right direction?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2018-12-14 09:31:39 +0200

nbruin gravatar image

Calling "show" on a graphics object doesn't return anything, so in your example, g3 is None, which explains the error. You can stuff the options into one of the plots:

plot1 = plot(-(x-1)^2+3, x, -3, 2.98, xmin=-2, xmax=10, ymin=-4, ymax=6,gridlines="minor")
...
g3=(plot1+plot2+plot3+pt1+pt2+pt3+pt4)

On that you should be able to call save:

g3.save("diagram.png")
g3.save("diagram.pdf")
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: 2018-12-13 18:57:55 +0200

Seen: 888 times

Last updated: Dec 14 '18