Repository: spyder-ide/spyder

Issue:
sympy shadows matplotlib in ipython console
_From [toddr...@gmail.com](https://code.google.com/u/110894839439148580880/) on 2013-12-05T13:56:49Z_

Spyder Version:  2.2.5
Python Version:  2.7.5
Qt Version    :  4.8.5, PyQt4 (API v2) 4.10.3 on Linux
IPython >=0.13  :  1.1.0 (OK)
matplotlib >=1.0:  1.3.1 (OK)
pep8 >=0.6      :  1.4.6 (OK)
pyflakes >=0.5.0:  0.7.3 (OK)
pylint >=0.25   :  1.0.0 (OK)
rope >=0.9.2    :  0.9.4 (OK)
sphinx >=0.6.6  :  1.2b3 (OK)
sympy >=0.7.0   :  0.7.3 (OK) 

What steps will reproduce the problem?
1. Enable "Support for Graphics (Matplotlib)" option in "iPython Console" > "Graphics"
   1. Enable "Use symbolic Math" option in "iPython Console" > "Advanced Settings"
   2. Open a new iPython console
   3. type "plot(arange(20))"

What is the expected output? 
A matplotlib figure with a diagonal line

What do you see instead?
Traceback (most recent call last):

  File "<ipython-input-4-ab8fea806eb1>", line 1, in <module>
    plot(arange(20))

  File "/usr/lib/python2.7/site-packages/sympy/plotting/plot.py", line 1195, in plot
    args = map(sympify, args)

  File "/usr/lib/python2.7/site-packages/sympy/core/sympify.py", line 256, in sympify
    rational=rational) for x in a])

MemoryError 

Please provide any additional information below

. This indicates that the sympy "plot" function is overriding the "matplotlib" one.  There is no indication from the preferences dialog that enabling symbolic math will also enable sympy plotting.  The dialog only talks about sympy in reference to symbolic operations and the output style.

_Original issue: http://code.google.com/p/spyderlib/issues/detail?id=1665_



Discussion:
_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-12-11T17:57:24Z_

This problem comes from this import statement

>>> from sympy import *

because Sympy's plot function is in the sympy namespace.

We could add a warning about it but this is not our fault, it's just how the Sympy devs decided to load it for interactive work.

**Status:** Accepted  
  **Labels:** -Cat-Settings -Component-IPython -Component-Logic Cat-IPythonConsole MS-v2.3.1  

_From [toddr...@gmail.com](https://code.google.com/u/110894839439148580880/) on 2014-01-14T07:04:05Z_

There are at least two approaches that could fix this:
1. Import sympy before pyplot or pylab, so matplotlib overwrites the sympy functions and not the other way around.
2. Only import the parts of sympy that are really needed rather than everything.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2014-01-14T07:18:33Z_

My thoughts about them:
1. That will break Sympy's plot function, so that's not a solution. We plan to disable pylab mode for 2.3, which will improve things a bit.
2. That's not for us to decide or fix, so please raise the issue with the Sympy devs.

_From [toddr...@gmail.com](https://code.google.com/u/110894839439148580880/) on 2014-01-17T04:50:50Z_
1. Either matloblib or sympy will have their plotting broken.  The problem I see is that sympy, which is not primarily a plotting package, has its plotting functionality given priority over matplotlib, which is by far the dominant plotting tool in python.  That seems backwards to me, I would think the more popular plotting tool would be given priority.
2. It has been raised.  Their suggestions were the same as mine, either import pylab second, or only import the parts of sympy you really need.

I think the best solution would be to separate out the options a bit.  sympy makes it easy to import the pretty printing independently from the symbolic manipulation. So that can be made a seperate option.  Then I would have an option to allow symbolic manipulation.  Finally, in the matplotlib option, I would allow the use to pick between matplotlib and sympy plotting if sympy plotting is really important enough to warrant its own option.

"We plan to disable pylab mode for 2.3, which will improve things a bit."

Why? This seems to be an extremely important option for someone wanting to use spyder as a matlab replacement.  In fact it is one of the main reasons I see people suggest using iPython in the first place, especially for people coming from matlab.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2014-01-17T07:53:37Z_
1. I see, so you want to use matplotlib (or more generally, pylab) and sympy _at the same time_. That makes things harder to sort out because I thought people would need to use one or the other but not both.
2. The problem is that by shadowing sympy's plot command you won't be able to plot symbolic functions. But there are other problems as well: sympy also shadows numpy's sin, cos, tan, etc.

About your best solution: I was also thinking to add more options to better control how we load sympy, but that's work for 2.4. However, the option we provide is not for power users, just for people who wants to easily solve integrals, derivatives, etc in Spyder. Power users can always load init_printing and the exact symbols they want to use to not conflict with pylab, but that's something a beginner can't sort out by himself.

About pylab: Sorry, I didn't express myself clearly. We are going to disable it _by default_, i.e. our IPython consoles won't come with pylab preloaded. But the option will be there (as it is right now) to enable it if you want to.

**Labels:** -MS-v2.3.1 MS-v2.4  

_From [toddr...@gmail.com](https://code.google.com/u/110894839439148580880/) on 2014-01-17T09:20:35Z_
1. There is no indication in the settings that this is the case, and I think giving the symbolic manipulation precedence is probably not the best approach.
2. So perhaps allowing people to enable both pylab and sympy at the same time is probably not a good idea.

"However, the option we provide is not for power users, just for people who wants to easily solve integrals, derivatives, etc in Spyder. Power users can always load init_printing and the exact symbols they want to use to not conflict with pylab, but that's something a beginner can't sort out by himself."

That is exactly mt point.  A beginner who sees that there is an option for plotting, and an option for symbolic manipulation, isn't going to understand that the two options conflict and that enabling symbolic manipulation will somehow break all the plotting examples they are trying to follow.  They will enable the options, try to plot, then probably get a weird error message about the number of data points without much hint where the error really lies.


Resolving patch:
diff --git a/spyder/widgets/ipythonconsole/shell.py b/spyder/widgets/ipythonconsole/shell.py
--- a/spyder/widgets/ipythonconsole/shell.py
+++ b/spyder/widgets/ipythonconsole/shell.py
@@ -101,7 +101,7 @@ def long_banner(self):
         mpl_installed = programs.is_module_installed('matplotlib')
         if mpl_installed and (pylab_o and autoload_pylab_o):
             pylab_message = ("\nPopulating the interactive namespace from "
-                             "numpy and matplotlib")
+                             "numpy and matplotlib\n")
             banner = banner + pylab_message
 
         # Sympy additions
@@ -114,6 +114,13 @@ def long_banner(self):
 >>> x, y, z, t = symbols('x y z t')
 >>> k, m, n = symbols('k m n', integer=True)
 >>> f, g, h = symbols('f g h', cls=Function)
+"""
+            banner = banner + lines
+        if (pylab_o and sympy_o):
+            lines = """
+Warning: pylab (numpy and matplotlib) and symbolic math (sympy) are both 
+enabled at the same time. Some pylab functions are going to be overrided by 
+the sympy module (e.g. plot)
 """
             banner = banner + lines
         return banner


Test patch: