Re: Matplotlib Show() on Sage command line on Mac OS X

208 views
Skip to first unread message

P Purkayastha

unread,
Nov 19, 2012, 1:21:08 AM11/19/12
to sage-...@googlegroups.com
On 11/19/2012 10:41 AM, Eric Verner wrote:
> When I try to plot data using matplotlib from the sage command line using show(), nothing happens. I have done some research and tried several different backends to matplotlib in the matplotlibrc file, including Cairo, GTK, WX, Agg, and MacOSX, and nothing works. I have tried to install pygtk and pycairo to Sage with no luck. Can someone give me a concise, straightforward way to get show() to work on my computer (Mac OS X 10.6.8). I would be very grateful. Thanks.
>

You can't, AFAIK. Matplotlib figures are not Sage objects. When you use
show(some_figure), it usually calls some_figure.show() which is a Sage
method.

One option for you would be to declare a simple class which takes a
matplotlib object and which has a show() method. In the show() method
you can save your figure to some temporary file and then call some image
viewer to view the file. This applies only for the command line. Look at
the very end of the output when you run the following commands, on how
to call your image viewer:
sage: p = Graphics()
sage: p.show??

Also, ticket #5128 would be of interest to you:
http://trac.sagemath.org/5128


On a notebook, you simply need to save the figure and it will
automatically be shown on the cell output. So, you would do:

<do all your matplotlib commands>
<let's suppose, for simplicity you were using pyplot>
plt.savefig('./figure.png')

and it will show up in your cell output.


Eric Verner

unread,
Nov 19, 2012, 7:26:37 AM11/19/12
to sage-...@googlegroups.com

P,
Thanks a lot for your suggestions. Yea, figuring this out seemed way too hard. I guess I'm not surprised that there is no way to do this. I am using Agg now, which saves the file. I started using Notebook(), and it is a really nice interface for writing code. I am somewhat surprised that Sage doesn't include a standard way to do this. I can plot lists using the standard Sage plot function, so I found it odd that I couldn't do the same with numpy arrays. If Sage could create interactive figures that were similar to Matlab figures, that would be pretty awesome. I took a look at the other ticket you suggested, and I will try it out.

Thanks!
Eric

Andrea Lazzarotto

unread,
Nov 19, 2012, 8:27:15 AM11/19/12
to sage-...@googlegroups.com


2012/11/19 Eric Verner <eve...@gmail.com>

If Sage could create interactive figures that were similar to Matlab figures

I don't know about Matlab figures, but with the Sage Notebook you can have interactive plots.

--
Andrea Lazzarotto - http://andrealazzarotto.com

Sébastien Labbé

unread,
Nov 19, 2012, 9:06:58 AM11/19/12
to sage-...@googlegroups.com
I had the same problem a year a go when using matplotlib from within Sage. I just look in my code and saw that I used savefig instead :

#plt.show()
plt.savefig('file.png')

Sébastien

P Purkayastha

unread,
Nov 19, 2012, 10:16:05 AM11/19/12
to sage-...@googlegroups.com
On 11/19/2012 09:27 PM, Andrea Lazzarotto wrote:
>
>
> 2012/11/19 Eric Verner <eve...@gmail.com <mailto:eve...@gmail.com>>
>
> If Sage could create interactive figures that were similar to Matlab
> figures
>
>
> I don't know about Matlab figures, but with the Sage Notebook you can
> have interactive plots.

I am pretty sure Eric is talking about plots with which you can interact
(like dynamically zoom, annotate, label, and even change the properties
of the plot), and not the kind of interactivity associated with 3D
plots. This feature is absent from Sage, and is not easy to implement
either (afaik).


kcrisman

unread,
Nov 19, 2012, 10:27:20 AM11/19/12
to sage-...@googlegroups.com
It used to be the case (when you could search sagenb.org, sigh) that there was a demo of "flot" with exactly this capability.  The new Jmol version has some of this for 3D plots, though not dynamic labeling etc. 

P Purkayastha

unread,
Nov 19, 2012, 10:54:53 AM11/19/12
to sage-...@googlegroups.com
On 11/19/2012 11:27 PM, kcrisman wrote:
>
>
> On Monday, November 19, 2012 10:16:23 AM UTC-5, P Purkayastha wrote:
>
> On 11/19/2012 09:27 PM, Andrea Lazzarotto wrote:
> >
> >
> > 2012/11/19 Eric Verner <eve...@gmail.com <javascript:>
> <mailto:eve...@gmail.com <javascript:>>>
> >
> > If Sage could create interactive figures that were similar to
> Matlab
> > figures
> >
> >
> > I don't know about Matlab figures, but with the Sage Notebook you
> can
> > have interactive plots.
>
> I am pretty sure Eric is talking about plots with which you can
> interact
> (like dynamically zoom, annotate, label, and even change the properties
> of the plot), and not the kind of interactivity associated with 3D
> plots. This feature is absent from Sage, and is not easy to implement
> either (afaik).
>
>
> It used to be the case (when you could search sagenb.org, sigh) that
> there was a demo of "flot" with exactly this capability. The new Jmol
> version has some of this for 3D plots, though not dynamic labeling etc.

Just looked up flot. Was the demo a standalone plotter, or was it
integrated with matplotlib? Given the huge amount of functionality
provided by matplotlib, I think it doesn't make much sense to revert to
something simpler. Perhaps it would be easier if we could hook into the
interactive features of matplotlib itself.


P Purkayastha

unread,
Nov 19, 2012, 12:10:27 PM11/19/12
to sage-...@googlegroups.com
On 11/19/2012 08:26 PM, Eric Verner wrote:
> I can plot lists using the standard Sage plot function, so I found it odd that I couldn't do the same with numpy arrays.

This is #11787 ( http://trac.sagemath.org/11787 ). It has a trivial fix
that is already mentioned in the description. But I think the trivial
fix can be slow if the number of plot points is large.

kcrisman

unread,
Nov 19, 2012, 1:31:27 PM11/19/12
to sage-...@googlegroups.com

>
> It used to be the case (when you could search sagenb.org, sigh) that
> there was a demo of "flot" with exactly this capability.  The new Jmol
> version has some of this for 3D plots, though not dynamic labeling etc.

Just looked up flot. Was the demo a standalone plotter, or was it
integrated with matplotlib? Given the huge amount of functionality
provided by matplotlib, I think it doesn't make much sense to revert to
something simpler. Perhaps it would be easier if we could hook into the
interactive features of matplotlib itself.


I think the idea was that it would be an alternate backend if people wanted this functionality.  Also apparently at one time the idea was that the plotting code would be more backend-agnostic, so it would require some care to back engineer that... but not horrible.


Building an interactive mpl instance would be great; I know nothing about mpl's capabilities in this regard.

Jason Grout

unread,
Nov 19, 2012, 3:27:55 PM11/19/12
to sage-...@googlegroups.com
On 11/19/12 12:31 PM, kcrisman wrote:
>
> >
> > It used to be the case (when you could search sagenb.org
> <http://sagenb.org>, sigh) that
Check out
http://mdboom.github.com/blog/2012/10/11/matplotlib-in-the-browser-its-coming/
to see work-in-progress towards interactive matplotlib in the browser.

Thanks,

Jason



Reply all
Reply to author
Forward
0 new messages