JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
474 stars 87 forks source link

clearer error message for default MacOS matplotlib #103

Open trbedwards opened 9 years ago

trbedwards commented 9 years ago

I've imported the PyPlot module like so: using PyCall pygui(:tk) using PyPlot

I then try to use the plot function: x=1:10; y=sin(x); plot(x,y)

But I get this error: pltm not defined while loading In[14], in expression starting on line 1

in abstract_eval_global at ./inference.jl:1027 in abstract_eval_symbol at ./inference.jl:1066 in abstract_eval at ./inference.jl:919 in abstract_eval_arg at ./inference.jl:866 in abstract_eval_call at ./inference.jl:878 in abstract_eval at ./inference.jl:935 in abstract_eval_arg at ./inference.jl:866 in abstract_eval_call at ./inference.jl:878 in abstract_eval at ./inference.jl:935 in abstract_interpret at ./inference.jl:1107 in typeinf at ./inference.jl:1409 in typeinf at /Users/tre11/git/julia/usr/lib/julia/sys.dylib in typeinf_ext at ./inference.jl:1216

This is in the latest stable version of Julia 0.3.4-pre+8, and latest stable versions of python, ipython, matpplotlib, etc.

stevengj commented 9 years ago

Works fine for me in both Julia 0.3 and Julia 0.4 on MacOS. Can you try Pkg.checkout("PyCall") and Pkg.checkout("PyPlot") to see if the latest master versions fix this?

trbedwards commented 9 years ago

I've just tried re-compiling julia on the master branch, but now I have this error: config.status: executing libtool commands configure: summary of build options:

Version: GNU MP 6.0.0 Host type: x86_64-apple-darwin14.0.0 ABI: 64 Install prefix: /Users/tre11/git/julia/usr Compiler: clang -stdlib=libc++ -mmacosx-version-min=10.7 -m64 Static libraries: no Shared libraries: yes

make[1]: *** [julia-release] Error 2 make: *** [release] Error 2

stevengj commented 9 years ago

My suggestion was to check out the master versions of PyPlot and PyCall, not compile the latest Julia. Julia 0.3.x should be fine.

trbedwards commented 9 years ago

So I re-installed Julia (using the latest stable pre-compiled binary for OS X), installed the PyCall and PyPlot packages then checked out the master versions using Pkg.checkout("PyCall") and Pkg.checkout("PyPlot"). I get a slightly different error this time:

ERROR: pltm not defined in abstract_eval_global at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_symbol at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_interpret at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in typeinf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times) in abstract_call_gf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times) in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib in typeinf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times) in typeinf_ext at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib

I should also add that when I importing the PyPlot module with using PyPlot I get the following error: Warning: error initializing module PyPlot: PyCall.PyError(msg="PyImport_ImportModule", T=PyCall.PyObject(o=0x00000000118dd698), val=PyCall.PyObject(o=0x0000000013d914d0), traceback=PyCall.PyObject(o=0x0000000013d7fdd0))

trbedwards commented 9 years ago

Ok so I managed to get rid of the errors by installing qt with brew install qt then entering pygui(:qt) after using PyCall. However, when I plot something, nothing appears. Is there some further setting up I need to do to display the plot? I've tried both in julia and ipython console --profile julia. Thanks

stevengj commented 9 years ago

What happens if you do

using PyCall
pyimport("matplotlib.pyplot")

?

stevengj commented 9 years ago

(BTW, I tend to recommend Anaconda Python rather than Homebrew; I've found Homebrew to be rather fragile for Python packages.)

trbedwards commented 9 years ago

I got it working like this:

using PyCall
@pyimport matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()
trbedwards commented 9 years ago

Thanks a lot, just a quick question. Is there a way to select the plot window after it's been generated? If I focus on another application then I cannot cmd-tab to the plot window unless I click and drag other windows out of the way. Sometime I lose the window and have to restart julia.

stevengj commented 9 years ago

@trbedwards, @pyimport matplotlib.pyplot as plt is working because it defaults to the MacOS (Cocoa) backend. (No, I don't know how to force it to bring the plot window to the front; maybe file an issue with Matplotlib, assuming the same thing happens in Python.)

You'd ideally like to get PyPlot working in order to get interactive plots (so that you don't need show and can use the REPL and the plot window simultaneously) or for IJulia. However, PyPlot doesn't support the native MacOS backend (see #11), so you have to have a Python with a working Tk or Qt GUI backend for Matplotlib.

Unfortunately, the Matplotlib that ships with MacOS doesn't have a working Qt or Tk backend as far as I can tell, so you need to install a different Matplotlib to use the PyPlot module. I recommend installing Anaconda Python (which will give you Matplotlib, SciPy, etc.).

stevengj commented 9 years ago

Actually, I looked into this further, and it seems that the Tk backend does work with the built-in Matplotlib in MacOS, but it requires that you install XQuartz.

So, just install XQuartz and PyPlot should work.

Re-opening since we should really have a clearer error message for this.

trbedwards commented 9 years ago

I should mention that I have XQuartz installed and still encounter the error if I use the Tk backend via

using PyCall
pygui(:tk)
using PyPlot

rather than

using PyCall
@pyimport matplotlib.pyplot as plt
stevengj commented 9 years ago

What is the exact, full error output that you get with the Tk backend in this case?

trbedwards commented 9 years ago

After typing this:

using PyCall
pygui(:tk)
using PyPlot

I get

Warning: error initializing module PyPlot:
PyCall.PyError(msg=":PyImport_ImportModule", T=PyCall.PyObject(o=0x000000000b364698), val=PyCall.PyObject(o=0x000000000d914680), traceback=PyCall.PyObject(o=0x000000000d904f80))

Then when I try to plot something:

x=1:10; y=sin(x);
plot(x,y)

I get this:

ERROR: pltm not defined
 in abstract_eval_global at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_symbol at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_interpret at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in typeinf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times)
 in abstract_call_gf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times)
 in abstract_eval_call at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in abstract_eval_arg at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
 in typeinf at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 2 times)
 in typeinf_ext at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/julia/sys.dylib
stevengj commented 9 years ago

What is the output if you do:

using PyCall
matplotlib = pyimport("matplotlib")
matplotlib[:use]("tkagg")
pltm = pyimport("matplotlib.pyplot")

and what is the output of versioninfo(true)?

stevengj commented 9 years ago

Note also that if you upgraded MacOS since you installed XQuartz, you may need to re-install XQuartz. (I've noticed that many MacOS upgrades tend to blow away part of the XQuartz installation.)

trbedwards commented 9 years ago

The output after the last command of

using PyCall
matplotlib = pyimport("matplotlib")
matplotlib[:use]("tkagg")
pltm = pyimport("matplotlib.pyplot")

is

ERROR: PyError (:PyImport_ImportModule) <type 'exceptions.ImportError'>
ImportError('cannot import name _tkagg',)
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/backends/backend_tkagg.py", line 11, in <module>
    import matplotlib.backends.tkagg as tkagg
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/backends/tkagg.py", line 2, in <module>
    from matplotlib.backends import _tkagg

 in pyerr_check at /Users/tre11/.julia/v0.3/PyCall/src/exception.jl:58
 in pyimport at /Users/tre11/.julia/v0.3/PyCall/src/PyCall.jl:91

The output of versioninfo(true) is:

Julia Version 0.3.3
Commit b24213b* (2014-11-23 20:19 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin13.3.0)
  CPU: Intel(R) Core(TM) i5-2415M CPU @ 2.30GHz
  WORD_SIZE: 64
  uname: Darwin 14.0.0 Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64 i386
Memory: 8.0 GB (537.08203125 MB free)
Uptime: 1.225878e6 sec
Load Avg:  2.22021484375  2.21044921875  2.31005859375
Intel(R) Core(TM) i5-2415M CPU @ 2.30GHz:
       speed         user         nice          sys         idle          irq
#1  2300 MHz     473136 s          0 s     321060 s    2348755 s          0 s
#2  2300 MHz     170519 s          0 s      82088 s    2843067 s          0 s
#3  2300 MHz     456230 s          0 s     200447 s    2439001 s          0 s
#4  2300 MHz     175719 s          0 s      86306 s    2833647 s          0 s

  BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.3
Environment:
  PATH = /Applications/Julia-0.3.3.app/Contents/Resources/julia/bin:/Applications/Julia-0.3.3.app/Contents/Resources/julia/libexec/git-core:/Applications/MATLAB_R2014a.app/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/texbin:/Users/tre11/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/texbin
  XPC_FLAGS = 0x0
  HOME = /Users/tre11
  TERM = xterm-256color
  FONTCONFIG_PATH = /Applications/Julia-0.3.3.app/Contents/Resources/julia/etc/fonts
  GIT_EXEC_PATH = /Applications/Julia-0.3.3.app/Contents/Resources/julia/libexec/git-core
  PYTHONHOME = /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7:/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7

Package Directory: /Users/tre11/.julia/v0.3
7 required packages:
 - Gadfly                        0.3.9
 - IJulia                        0.1.16
 - PyCall                        0.7.0              master
 - PyPlot                        1.4.3+             master
 - Roots                         0.1.9
 - SymPy                         0.2.20
 - Winston                       0.11.6
43 additional packages:
 - ArrayViews                    0.4.8
 - BinDeps                       0.3.7
 - Cairo                         0.2.21
 - Calculus                      0.1.5
 - Codecs                        0.1.2
 - Color                         0.3.14
 - Compat                        0.2.5
 - Compose                       0.3.9
 - Contour                       0.0.6
 - DataArrays                    0.2.5
 - DataFrames                    0.5.12
 - DataStructures                0.3.5
 - Dates                         0.3.2
 - Distances                     0.2.0
 - Distributions                 0.6.1
 - DualNumbers                   0.1.1
 - FixedPointNumbers             0.0.6
 - GZip                          0.2.13
 - Grid                          0.3.6
 - Hexagons                      0.0.2
 - Homebrew                      0.1.11
 - ImmutableArrays               0.0.6
 - IniFile                       0.2.4
 - Iterators                     0.1.7
 - JSON                          0.3.9
 - KernelDensity                 0.1.0
 - LaTeXStrings                  0.1.2
 - Loess                         0.0.3
 - NaNMath                       0.0.2
 - Nettle                        0.1.7
 - Optim                         0.4.0
 - PDMats                        0.3.1
 - Polynomials                   0.0.3
 - PowerSeries                   0.1.11
 - REPLCompletions               0.0.3
 - Reexport                      0.0.2
 - SHA                           0.0.3
 - Showoff                       0.0.2
 - SortingAlgorithms             0.0.2
 - StatsBase                     0.6.10
 - Tk                            0.2.16
 - URIParser                     0.0.3
 - ZMQ                           0.1.15