Displaying plots in inline mode for IPython notebook

890 views
Skip to first unread message

Jose Guzman

unread,
May 31, 2013, 9:41:34 AM5/31/13
to sage-s...@googlegroups.com
Hi everyboydy!


Is there any way to display Sage plots in inline mode? I've tried sage
ipython notebook -pylab=inline but It did not work.


Declaring %pylab inline does not help, for example:

>>> from sage.all import*
>>> t = var('t')
>>> plot(sin(t), 0, 2*pi)

n _plot_args(self, tup, kwargs)
290 tup = tup[:-1]
291 elif len(tup) == 3:
--> 292 raise ValueError('third arg must be a format string')
293 else:
294 linestyle, marker, color = None, None, None

ValueError: third arg must be a format string

Thanks!

Jose

--
Jose Guzman
http://www.ist.ac.at/~jguzman/

P Purkayastha

unread,
Jun 2, 2013, 5:34:06 AM6/2/13
to sage-s...@googlegroups.com
In Sage the plots appear "inline", but the plots are saved as files in a directory structure within the working worksheet directory. From my reading here:
it appears that the ipython notebook saves everything inside a single json file, which is probably also how one gets everything "inline". I am just thinking out loud; not sure if this is exactly how it works - but it does look like it is not compatible with how the Sage plots work.

There are two possible solutions:
1. Find out how matplotlib plots are handled. They must be saved in some directory as png files before they are displayed in the ipython notebook. If you can find that directory, then give that path along with a file name to the Sage's plot command, like this:

plot(x).save('/path/to/directory/file.png')


2. Use Sage to plot, but at the last step, take the matplotlib Figure object out of sage. Then use some direct matplotlib calls to show this figure (I don't know exactly what they should be), like this:

p = plot(x) + plot(x^2) + list_plot(range(5))
pm
= p.matplotlib(<pass other options as you see fit>) # This is a matplotlib Figure object
from matplotlib import pyplot as plt
# Some matplotlib commands which can incorporate this Figure object into the plt object
plt
.show()

Jose Guzman

unread,
Jun 6, 2013, 3:18:54 AM6/6/13
to sage-s...@googlegroups.com
This was a very good idea!

Now I generate the plots as you mentioned:

The trick was to use IPython.display to take the png files. Like this:


x = var('x')
plot
(sin(x), 0, 2*pi).save('/path/to/directory/file.png')
from IPython.display import display, Image
display(Image(
'/path/to/directory/file.png'))

sat...@gmail.com

unread,
Sep 25, 2014, 11:04:45 PM9/25/14
to sage-s...@googlegroups.com
Hello,

Would you teach me how to change image size ?

Thanks!

Yoshihiro Sato

P Purkayastha

unread,
Sep 27, 2014, 10:57:52 PM9/27/14
to sage-s...@googlegroups.com

sat...@gmail.com

unread,
Sep 28, 2014, 10:47:19 PM9/28/14
to sage-s...@googlegroups.com
Hi,

I made an example.

    from IPython.display import display, Image
    x = var('x')
    plot(sin(x), 0, 2*pi, figsize= 4).save('/path/to/directory/file.png')
    display(Image(
'/path/to/directory/file.png'))

Thanks. 
 y. Sato

sat...@gmail.com

unread,
Oct 10, 2014, 12:54:07 AM10/10/14
to sage-s...@googlegroups.com
Hi,
I have an idea using ImageMagic.

In terminal:
$ convert -resize x350 file.png file2.png

In notebook:

from IPython.display import display, Image
display(Image('/path/to/directory/file2.png'))

Would you please show me  how to do "convert -resize x350 file.png file2.png" in notebook ?

I usually input the following.
$ sage -ipython notebook
from sage.all import *
    ...

Thanks,
Y. Sato

2014年9月29日月曜日 11時47分19秒 UTC+9 sat...@gmail.com:

P Purkayastha

unread,
Oct 10, 2014, 1:05:07 AM10/10/14
to sage-s...@googlegroups.com

You can use os.system:

import os
os.system("convert..... ")

--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/9pUhO8wO4nE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

sat...@gmail.com

unread,
Oct 14, 2014, 5:36:33 AM10/14/14
to sage-s...@googlegroups.com
Hello,

Thanks you !

Y.Sato

Reply all
Reply to author
Forward
0 new messages