1

I'm using

  • MacOS 10.13.6, High Sierra
  • From terminal, julia -- version yields julia version 1.0.1
  • From terminal, python --version yields Python 2.7.15
  • Running scripts from terminal

I have a small Julia script that works after a fix that I found from googling:

File: standalone.jl (works)

using PyCall

@pyimport matplotlib
matplotlib.use("TkAgg") # Required on Mac, otherwise I get an error: "-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85e1050280...."
@pyimport matplotlib.pyplot as plt

x = [1, 2]
y = [1, 2]

plt.figure()
plt.plot(x, y)
plt.show()

I would like to know how to re-cast this script into a module file and a main file in the following way (adjusted from the documentation):

File: getplt.jl

__precompile__() # this module is safe to precompile
module getplt
using PyCall

const plt = PyNULL()
const mpl = PyNULL()

function __init__()
  copy!(plt, pyimport_conda("matplotlib.pyplot", "matplotlib"))
  copy!(mpl, pyimport_conda("matplotlib", "matplotlib"))
end

end

File: main.jl

push!(LOAD_PATH, "./")
using getplt

getplt.mpl[:use]("TkAgg") # Required on Mac

x = [1, 2]
y = [0, 2]

getplt.plt[:figure]
getplt.plt[:plot](x, y)
getplt.plt[:show]

When I tried doing this, I get the following error:

-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f9e78221710
2018-10-30 16:11:44.897 julia[87599:2109977] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f9e78221710'
*** First throw call stack:
(
  0   CoreFoundation                      0x00007fff4697f2db __exceptionPreprocess + 171
  1   libobjc.A.dylib                     0x00007fff6db22c76 objc_exception_throw + 48
  2   CoreFoundation                      0x00007fff46a17db4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
  3   CoreFoundation                      0x00007fff468f5820 ___forwarding___ + 1456
  4   CoreFoundation                      0x00007fff468f51e8 _CF_forwarding_prep_0 + 120
  5   libtk8.6.dylib                      0x0000000134d5531d TkpInit + 413
  6   libtk8.6.dylib                      0x0000000134cad17e Initialize + 2622
  7   _tkinter.cpython-37m-darwin.so      0x0000000134295a0f _tkinter_create + 1183
  8   libpython3.7m.dylib                 0x000000012201dc29 _PyMethodDef_RawFastCallKeywords + 681
  9   libpython3.7m.dylib                 0x000000012201dd2d _PyCFunction_FastCallKeywords + 45
  10  libpython3.7m.dylib                 0x0000000122127714 call_function + 612
  11  libpython3.7m.dylib                 0x000000012211e7fa _PyEval_EvalFrameDefault + 8138
  12  libpython3.7m.dylib                 0x000000012211c399 _PyEval_EvalCodeWithName + 3305
  13  libpython3.7m.dylib                 0x000000012201cb21 _PyFunction_FastCallDict + 481
  14  libpython3.7m.dylib                 0x000000012201e514 _PyObject_Call_Prepend + 164
  15  libpython3.7m.dylib                 0x0000000122085eda slot_tp_init + 298
  16  libpython3.7m.dylib                 0x000000012208f5d7 type_call + 295
  17  libpython3.7m.dylib                 0x000000012201d7e3 _PyObject_FastCallKeywords + 691
  18  libpython3.7m.dylib                 0x000000012212777c call_function + 716
  19  libpython3.7m.dylib                 0x000000012211e927 _PyEval_EvalFrameDefault + 8439
  20  libpython3.7m.dylib                 0x000000012201d3de function_code_fastcall + 254
  21  libpython3.7m.dylib                 0x0000000122127786 call_function + 726
  22  libpython3.7m.dylib                 0x000000012211e7fa _PyEval_EvalFrameDefault + 8138
  23  libpython3.7m.dylib                 0x000000012211c399 _PyEval_EvalCodeWithName + 3305
  24  libpython3.7m.dylib                 0x000000012201cb21 _PyFunction_FastCallDict + 481
  25  libpython3.7m.dylib                 0x000000012201e514 _PyObject_Call_Prepend + 164
  26  libpython3.7m.dylib                 0x000000012201ff24 method_call + 36
  27  libpython3.7m.dylib                 0x000000012201df26 PyObject_Call + 246
  28  libpython3.7m.dylib                 0x000000012211eb73 _PyEval_EvalFrameDefault + 9027
  29  libpython3.7m.dylib                 0x000000012211c399 _PyEval_EvalCodeWithName + 3305
  30  libpython3.7m.dylib                 0x000000012201d966 _PyFunction_FastCallKeywords + 230
  31  libpython3.7m.dylib                 0x0000000122127786 call_function + 726
  32  libpython3.7m.dylib                 0x000000012211e88e _PyEval_EvalFrameDefault + 8286
  33  libpython3.7m.dylib                 0x000000012201d3de function_code_fastcall + 254
  34  libpython3.7m.dylib                 0x0000000122127786 call_function + 726
  35  libpython3.7m.dylib                 0x000000012211e88e _PyEval_EvalFrameDefault + 8286
  36  libpython3.7m.dylib                 0x000000012211c399 _PyEval_EvalCodeWithName + 3305
  37  libpython3.7m.dylib                 0x000000012201d966 _PyFunction_FastCallKeywords + 230
  38  libpython3.7m.dylib                 0x0000000122127786 call_function + 726
  39  libpython3.7m.dylib                 0x000000012211e88e _PyEval_EvalFrameDefault + 8286
  40  libpython3.7m.dylib                 0x000000012211c399 _PyEval_EvalCodeWithName + 3305
  41  libpython3.7m.dylib                 0x000000012201cb21 _PyFunction_FastCallDict + 481
  42  ???                                 0x0000000121cad41a 0x0 + 4861907994
  43  ???                                 0x0000000121cb20c4 0x0 + 4861927620
  44  ???                                 0x0000000121cae6d4 0x0 + 4861912788
  45  ???                                 0x0000000121cae633 0x0 + 4861912627
  46  libjulia.1.0.dylib                  0x0000000104848ac3 jl_fptr_trampoline + 51
  47  libjulia.1.0.dylib                  0x00000001048594b2 jl_f__apply + 1218
  48  ???                                 0x0000000121cae4ee 0x0 + 4861912302
  49  libjulia.1.0.dylib                  0x0000000104848ac3 jl_fptr_trampoline + 51
  50  libjulia.1.0.dylib                  0x0000000104a0dab8 do_call + 200
  51  libjulia.1.0.dylib                  0x0000000104a0c5ac eval_body + 1228
  52  libjulia.1.0.dylib                  0x0000000104a0caf8 jl_interpret_toplevel_thunk_callback + 408
  53  libjulia.1.0.dylib                  0x000000010485f68c enter_interpreter_frame + 28
  54  libjulia.1.0.dylib                  0x00000001048796c2 jl_toplevel_eval_flex + 1282
  55  libjulia.1.0.dylib                  0x0000000104854ebf jl_parse_eval_all + 1279
  56  libjulia.1.0.dylib                  0x000000010487a9e3 jl_load_ + 115
  57  sys.dylib                           0x000000010d96c9fa japi1_include_relative_2907.clone_1.clone_2 + 1178
  58  sys.dylib                           0x000000010d45a18d japi1_include_3913.clone_1 + 189
  59  sys.dylib                           0x000000010d459ea0 japi1__start_2552.clone_1 + 704
  60  julia                               0x000000010483b48d true_main + 1341
  61  julia                               0x000000010483aecc main + 108
  62  libdyld.dylib                       0x00007fff6e73c015 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

signal (6): Abort trap: 6
in expression starting at /Users/.../main.jl:10
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 6553408 (Pool: 6552271; Big: 1137); GC: 14
Abort trap: 6

Please, let me know if any further information is needed, and any help in overcoming this error is greatly appreciated.

Update

My tentative solution, which doesn't answer this question, is to simply use the Plots package (which does work inside modules) and not using PyPlot. I'm going to leave this question open for the time being, as I may like to use matplotlib.pyplot sometime in the future and I hope that this error is resolved.

Charles
  • 947
  • 1
  • 15
  • 39

2 Answers2

0

Here are the Codes and Documentation of PyPlot

https://github.com/JuliaPy/PyPlot.jl

I recommended you to use the PyPlot Library. Plotting for Julia based on matplotlib.pyplot.

Use the commands (Do this in terminal mode, IPython sometimes crushes)

using Pkg

Pkg.add("PyCall") Pkg.add("PyPlot")

I think this solves your problem Pkg.build("PyCall")
Pkg.build("PyPlot")

using PyCall
using PyPlot

Sorry for my bad English.

Derek Ribeiro
  • 108
  • 1
  • 8
  • Please see the comment I left on @Przemyslaw's answer. I believe the same applies to this answer, unfortunately. – Charles Oct 31 '18 at 21:34
  • 1
    You must use Plots.jl, It's solve your ploblem, and is a native lib of Julia. I will share you, a example using Plots. In this [link](https://drive.google.com/open?id=1CkzV4Wxqpll_gb8Nd43O7OawrEJg3vSZ). Enjoy it. – Derek Ribeiro Nov 01 '18 at 04:27
  • If you have some problem using MAC and Plots.jl, see this [link](https://github.com/JuliaPlots/Plots.jl/issues/571). Perhaps solve you problem. – Derek Ribeiro Nov 01 '18 at 04:37
0

You do not have to use PyPlot.jl to use matplotlib in Julia.

However, you have to import it before calling any matplotlib operation via PyCall. Follow the steps below:

(1) Install PyPlot module with the Julia package manager (press ] and run add PyPlot)

(2) Add a new line at the beginning of your code:

using PyPlot

Now the calls to matplolib will not crash Julia. The line matplotlib.use("TkAgg") might not be required (I do not have a Mac to test).

Here is a MWE with a module:

module M1
using PyCall
using PyPlot
@pyimport matplotlib.pyplot as pyplt
function makeplot()    
    pyplt.plot(1:10,(1:10).^2)
end
end

using Main.M1
M1.makeplot()

Having that said consider using Plots.jl for plotting - this allows you have a single code in Julia that plots on several backends. When making choices regarding to plotting I always find this page very helpfull: http://docs.juliaplots.org/latest/backends/.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • I've tried simply `using PyPlot`. This works in the REPL, but not when used from a module (I've updated my link to be more specific). In case I'm wrong, could you provide a minimum working example? – Charles Oct 31 '18 at 21:33
  • I provided MWE - works on Windows (I do not have a Mac) but it should work too (the key is not to forget to import PyPlot) – Przemyslaw Szufel Oct 31 '18 at 23:09
  • Hm, this must be an issue related to Mac. When I try running this, I get the same looking error in my post. – Charles Oct 31 '18 at 23:20