very basic question about plotting and matplotlib

44 views
Skip to first unread message

Pierre Guillot

unread,
Jul 1, 2015, 12:21:39 PM7/1/15
to sy...@googlegroups.com
Hi !

I've been using sympy for a while, mostly through Sage but sometimes also on its own. For a number of reasons, I have decided to use sympy with my students next fall, and i need to learn a few basic things, for which I normally rely on Sage.

Mostly, I mean plotting. All my plots are normally done with Sage, and matplotlib is another library which I have never used independently of Sage... I have looked at sympy's documentation but failed to find an A to Z guide to plotting (I guess it is assumed that the user knows matplotlib better than I do).

Here are the things I have tried -- the first worked but is not ideal, the others failed.

(1) The Sage cell at http://sagecell.sagemath.org/. I select python as a language, then enter

from sympy import *

x= Symbol("x")
plot(x*x)

and it works !



(2) Sympy live at http://live.sympy.org/.

I have tried plot(x*x) and plot(x*x).show() (I had my hopes on the second one) but nothing pops up. That's disapppointing, as I wanted to advertise the sympy live functionality to my students.



(3) On my local machine. I go "sage -ipython" which launches a copy of python with all the libraries that you could think of, certainly sympy and matplotlib.

Again plot(x*x) and plot(x*x).show() fail to display anything.

I'm guessing in this case it's a (simple?) matter of telling sympy to talk to matplotlib, and perhaps of telling matplotlib to start itself. I have no idea how to proceed though!

Thank you for any help...

Pierre
PS I'm using Mac OSX.


Joachim Durchholz

unread,
Jul 1, 2015, 1:53:09 PM7/1/15
to sy...@googlegroups.com
> (3) On my local machine. I go "sage -ipython" which launches a copy of
> python with all the libraries that you could think of, certainly sympy and
> matplotlib.
>
> Again plot(x*x) and plot(x*x).show() fail to display anything.
>
> I'm guessing in this case it's a (simple?) matter of telling sympy to talk
> to matplotlib, and perhaps of telling matplotlib to start itself. I have no
> idea how to proceed though!

SymPy should automatically detect the presence of matplotlib and use it.
Do you have it installed locally?

Pierre Guillot

unread,
Jul 1, 2015, 5:15:47 PM7/1/15
to sy...@googlegroups.com
>SymPy should automatically detect the presence of matplotlib and use it.
>Do you have it installed locally?

well, "import matplotlib" works. And plots in sage work.

by the way, when plots in sympy "work", is it the same mechanism as in Sage, that is, the plot is saved to a temporary file and opened in a new window with the system's viewer? Perhaps I should have made it 100% clear that I'm using the command line, not a notebook or anything.

Jason Moore

unread,
Jul 1, 2015, 5:17:31 PM7/1/15
to sy...@googlegroups.com
Are you using the sympy plot command?

from sympy.plotting import plot

http://docs.sympy.org/dev/modules/plotting.html

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/58b30267-51c8-4a1e-9430-a2bcb90cfd64%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Pierre Guillot

unread,
Jul 1, 2015, 5:24:05 PM7/1/15
to sy...@googlegroups.com
I have just tried "from sympy.plotting import plot" but it does not
seem any different from the plot command that I had by doing "from
sympy import *" (as I indicated in my original post)
> You received this message because you are subscribed to a topic in the
> Google Groups "sympy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sympy/-j56ppfR7kw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/CAP7f1AiNbJ%3Dvi1cu%3D-Z_-vmTTXkK6iqwOa05kAR8beypEBN4Ew%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Pierre
06 06 40 72 28

Jason Moore

unread,
Jul 1, 2015, 5:34:41 PM7/1/15
to sy...@googlegroups.com
Can you try this:


$ python
>>> from sympy import *
>>> x = Symbol('x')
>>> plot(x*x)

Try using a "pure" python interpreter instead of whatever Sage is launching.

And the plots will not show up on sympy live. We do have some plotting functionality on sympy gamma though.

Pierre Guillot

unread,
Jul 1, 2015, 6:02:38 PM7/1/15
to sy...@googlegroups.com
> $ python
>>>> from sympy import *
>>>> x = Symbol('x')
>>>> plot(x*x)
>
> Try using a "pure" python interpreter instead of whatever Sage is launching.

On the one hand, normally (!) "sage -ipython" is really a pure python
interpreter ; on the other hand, my system's python does not have
matplotlib, so there's no way it's going to work.

Mind you, i've just tried on a linux machine under virtual box, it has
sympy and matplotlib working with the basic system's python, and it
all works fine!

So, as it turns out, you're right: "sage -ipython" does something
funny, with respect to matplotlib. It may be a normal feature, when
you think that under (standard) sage, the matplotlib plots do not open
a dedicated matplotlib window, just the system's image viewer, opened
on a temp .png file.

OK, so I understand pretty well what goes on. The thing is, I wanted
to tell my students that installing sympy+matplotlib (+numpy) was
super easy on any machine, and for Mac OSX my advice was to install
Sage, since installing matplotlib on a Mac is a f****ing nightmare
(last time I looked, anyway).

I wonder what Mac OSX users are advised to do, when wanting to work
with sympy+matplotlib: install anaconda?

anyway, thanks for your help. And yes, I noticed sympy gamma, it does do plots !

cheers
Pierre
> https://groups.google.com/d/msgid/sympy/CAP7f1AjiaCfWRmvaCAXjwt8CPoNEQymihXcOMboem_1t-tO%2BTQ%40mail.gmail.com.

Jason Moore

unread,
Jul 1, 2015, 6:09:38 PM7/1/15
to sy...@googlegroups.com
You should ask on the sage list about that. Maybe there is an option to revert back to normal matplotlib windows.

I would recommend having the students install anaconda. This works cross platform and all the scientific Python stuff just works. You can even install miniconda and only install sympy + matplotlib to avoid the giant Sage or Anaconda download.
Reply all
Reply to author
Forward
0 new messages