Saving parametric_plot3d() plots

44 views
Skip to first unread message

Fausto A. A. Barbuto

unread,
Jun 8, 2015, 8:17:29 PM6/8/15
to sage-s...@googlegroups.com

Hello,

Is there a command to save a plot generated by parametric_plot3d()?  Like, say:

P = parametric_plot3d([x,y,z],(u,-10.0,10.0),(v,-10.0,10.0),plot_points=[50,50],frame=False,color=(fc,colormaps.jet))
P.save('somename.png')

Thanks in advance for any light shed on this matter.

Fausto


kcrisman

unread,
Jun 8, 2015, 9:11:08 PM6/8/15
to sage-s...@googlegroups.com, fausto....@gmail.com, Volker Braun


On Monday, June 8, 2015 at 8:17:29 PM UTC-4, Fausto A. A. Barbuto wrote:

Hello,

Is there a command to save a plot generated by parametric_plot3d()?  Like, say:

P = parametric_plot3d([x,y,z],(u,-10.0,10.0),(v,-10.0,10.0),plot_points=[50,50],frame=False,color=(fc,colormaps.jet))
P.save('somename.png')


It would be helpful to see a 'minimal' example that one can just 'plug in'.    Ordinarily P.save should work fine.


My own puttering about led to this unfortunate mess.

sage: sage: var('u, v')
(u, v)
sage: sage: x = u
sage: sage: y = v
sage: z = u
sage: fc = sin(u)
sage: P = parametric_plot3d([x,y,z],(u,-10.0,10.0),(v,-10.0,10.0),plot_points=[50,50],frame=False,color=(fc,colormaps.jet))<snip expected deprecation warning>
  rich_output = self._call_rich_repr(obj, rich_repr_kwds)
/Users/.../sage/local/lib/python2.7/site-packages/sage/repl/rich_output/display_manager.py:570: RichReprWarning: Exception in _rich_repr_ while displaying object: the number of arguments must be less than or equal to 1
  RichReprWarning,
Graphics3d Object

the second warning was unexpected, the output REALLY is unexpected.

Volker (or someone else) what is going on here with the display hook - shouldn't *some* graphic, even an empty one, show up here, even if my made-up input makes no sense?  Once after trying something like this, Ctrl-C even led to a segfault, very impressive!  This is in beta3; in Sage 6.5 it's the possibly even worse KeyError: 'text/plain'.

Fausto A. A. Barbuto

unread,
Jun 8, 2015, 10:11:20 PM6/8/15
to sage-s...@googlegroups.com, vbrau...@gmail.com, fausto....@gmail.com


Hi,

Here is an example that works.  It plots an image.  What I had in mind was to replace P.show(...) by P.save(...) and, instead of displaying the image, saving it.
The replacement dDidn't work out though.  Needless to say, the script below runs on Sage's notebook.

Thanks for your reply.

Fausto

#---------- Script begins here ----------#
from time import time
u
,v = var('u','v')
a
= var('a')
a
= 0.2
a2
= (1.0 - a*a)
sa2
= sqrt(a2)
print "<html><h3>Breather Surface</h3></html>"

def x(u,v) :
   
return -u + (2.0*a2*cosh(a*u)*sinh(a*u))/(a*((a2*(cosh(a*u))^2 + a*a*(sin(sa2*v))^2)))
   
def y(u,v) :
   
return 2.0*sa2*cosh(a*u)*(-sa2*cos(v)*cos(sa2*v) - sin(v)*sin(sa2*v))/(a*((a2*(cosh(a*u))^2 + a*a*(sin(sa2*v))^2)))
   
def z(u,v) :
   
return 2.0*sa2*cosh(a*u)*(-sa2*sin(v)*cos(sa2*v) + cos(v)*sin(sa2*v))/(a*((a2*(cosh(a*u))^2 + a*a*(sin(sa2*v))^2)))

def fc(u,v) : # Colour scheme called by parametric_plot3d
   
return abs(u)/30.0
   
t1
= time()
P
= parametric_plot3d([x,y,z],(u,-30.0,30.0),(v,-160.0,160.0),plot_points=[250,250],frame=False,color=(fc,colormaps.jet))
P
.show(viewer='jmol')
t2
= time()
print "a = ",a
print "Execution time: ", t2-t1, " seconds."
#---------- Script ends here ----------#


Volker Braun

unread,
Jun 9, 2015, 3:39:40 AM6/9/15
to sage-s...@googlegroups.com, fausto....@gmail.com
On Tuesday, June 9, 2015 at 3:11:08 AM UTC+2, kcrisman wrote:
Volker (or someone else) what is going on here with the display hook -

parametric_plot does note perform adequate input checking, leading to the construction of a graphics3d object with bogous state.
 
shouldn't *some* graphic, even an empty one, show up here, even if my made-up input makes no sense?

I don't think so, the normal Python way is to show you a backtrace so you can fix your mistake and not make up random default return values. The problem here is of course that the color function fc must be a function of two arguments instead of one.

I can reproduce the crash, presumably you opened a bug for that?

kcrisman

unread,
Jun 9, 2015, 7:19:53 AM6/9/15
to sage-s...@googlegroups.com, vbrau...@gmail.com
Volker (or someone else) what is going on here with the display hook -

parametric_plot does note perform adequate input checking, leading to the construction of a graphics3d object with bogous state.
 
shouldn't *some* graphic, even an empty one, show up here, even if my made-up input makes no sense?

I don't think so, the normal Python way is to show you a backtrace so you can fix your mistake and not make up random default return values.

If there is an error, but that's not what (apparently) happened here, only a warning.  In which event it should raise an Error of some kind, but it didn't (did it?) and not return anything.
 
The problem here is of course that the color function fc must be a function of two arguments instead of one.


Ah, that's what happens when I try to check something when I'm too tired to read documentation - esp. of a ticket I was involved with (par. plot coloring), worse luck.  Good call.
 
I can reproduce the crash, presumably you opened a bug for that?

No, I couldn't get it to happen again - please do open a ticket if you were able to reproduce it.  Actually, I'd appreciate you opening and cc:ing me and Niles on a ticket about the color function checking as well (in a few minutes I'll be off the train in a no-internet zone for the day).  Thank you!

kcrisman

unread,
Jun 9, 2015, 7:24:47 AM6/9/15
to sage-s...@googlegroups.com, fausto....@gmail.com
Here is an example that works.  It plots an image.  What I had in mind was to replace P.show(...) by P.save(...) and, instead of displaying the image, saving it.
The replacement dDidn't work out though.  Needless to say, the script below runs on Sage's notebook.


As I expected, P.save() worked fine.  You do have to provide a filename/location.

sage: P.save('/Users/myname/test.png')

produced a nice file in exactly that location.  However, if you don't and just put a name, it will save the file in whatever directory, and if you don't provide an extension, it will save it as a "Sage object".

sage: P.save('hi')
sage: 
Exiting Sage (CPU time 0m25.67s, Wall time 3m23.43s).
gc04855:sage $ ls
COPYING.txt autom4te.cache config.status home src
Makefile bootstrap configure local tmp
README.txt build configure.ac logs upstream
VERSION.txt config dist m4
aclocal.m4 config.log hi.sobj sage

Note the "hi.sobj". Hope this helps!

Volker Braun

unread,
Jun 9, 2015, 8:01:28 AM6/9/15
to sage-s...@googlegroups.com, vbrau...@gmail.com
On Tuesday, June 9, 2015 at 1:19:53 PM UTC+2, kcrisman wrote:
I don't think so, the normal Python way is to show you a backtrace so you can fix your mistake and not make up random default return values.

If there is an error, but that's not what (apparently) happened here, only a warning.  In which event it should raise an Error of some kind, but it didn't (did it?) and not return anything.

A normal backtrace is also something that is generated as a special kind of output, and customized by IPython. The problem is that construction of the plot did not raise an error, even though it should have. Whenever you have an invalid object that fails while it prints itself you can't get the usual backtrace since you fail in the displayhook, and not while executing the command. This is why its such a bad idea to allow creation of bogous objects that then later fail over their feet when printing.

Fausto A. A. Barbuto

unread,
Jun 9, 2015, 12:51:00 PM6/9/15
to sage-s...@googlegroups.com, fausto....@gmail.com

On Tuesday, 9 June 2015 08:24:47 UTC-3, kcrisman wrote:
Here is an example that works.  It plots an image.  What I had in mind was to replace P.show(...) by P.save(...) and, instead of displaying the image, saving it.
The replacement dDidn't work out though.  Needless to say, the script below runs on Sage's notebook.


As I expected, P.save() worked fine.  You do have to provide a filename/location.

sage: P.save('/Users/myname/test.png')

Well, I did that since the beginning and yet no image was ever saved.  Could that be related to the fact that I run my script in the notebook?  You seem to run it in Sage's prompt.

Thanks,

Fausto

kcrisman

unread,
Jun 9, 2015, 7:55:55 PM6/9/15
to sage-s...@googlegroups.com, fausto....@gmail.com
sage: P.save('/Users/myname/test.png')

Well, I did that since the beginning and yet no image was ever saved.  Could that be related to the fact that I run my script in the notebook?  You seem to run it in Sage's prompt.


Ah, you didn't mention that!

Actually, it does still save.  But it will save, by default, in the directory associated to the cell you save it in, which can be annoying.  You should be able to do

P.save(DATA+test.png)

to save it in the "data" folder of the worksheet, or (assuming your permissions are normal)

P.save('/complete/path/to/folder.png') 

to save it in your filesystem in an arbitrary place.

At one point there was also a link to such saved files in the notebook below each cell, but after a rewrite those disappeared, and it is currently in relatively maintenance mode so we have not put the time in to reimplement that.
Reply all
Reply to author
Forward
0 new messages