在Julia模块中使用Julia PyCall来使用matplotlib.pyplot

时间:2018-10-30 23:14:50

标签: module julia precompile pyobject

我正在使用

  • MacOS 10.13.6,High Sierra
  • 从终端julia版本开始-版本产生了julia版本1.0.1
  • 在终端上,python --version产生Python 2.7.15
  • 从终端运行脚本

我有一个小的Julia脚本,可以在通过谷歌搜索找到的fix之后使用:

文件:standalone.jl(有效)

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()

我想知道如何通过以下方式(从documentation进行调整)将该脚本重新投射到模块文件和主文件中:

文件: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

文件: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]

尝试执行此操作时,出现以下错误:

-[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

请告诉我是否需要进一步的信息,对于克服此错误的任何帮助,我们将不胜感激。

更新

我的暂时解决方案(不能回答这个问题)是仅使用Plots程序包(可以在模块内部工作)而不使用PyPlot。我暂时暂时不提这个问题,因为将来我可能想使用matplotlib.pyplot,希望这个错误可以解决。

2 个答案:

答案 0 :(得分:0)

以下是PyPlot的代码和文档

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

我建议您使用PyPlot库。根据matplotlib.pyplot为Julia进行绘图。

使用命令(在终端模式下执行此操作,IPython有时会崩溃)

使用Pkg

Pkg.add(“ PyCall”) Pkg.add(“ PyPlot”)

我认为这可以解决您的问题 Pkg.build(“ PyCall”)
Pkg.build(“ PyPlot”)

使用PyCall
使用PyPlot

对不起,我的英语不好。

答案 1 :(得分:0)

在Julia中不必使用PyPlot.jl来使用matplotlib。

但是,您必须导入它,然后才能通过PyCall调用任何matplotlib操作。请按照以下步骤操作:

(1)使用Julia软件包管理器安装PyPlot模块(按]并运行add PyPlot)

(2)在代码开头添加新行:

using PyPlot

现在,对matplolib的呼叫不会崩溃Julia。可能不需要行matplotlib.use("TkAgg")(我没有要测试的Mac)。

这是带有模块的MWE:

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()

话虽如此,请考虑使用Plots.jl进行绘图-这使您可以在Julia中使用一个代码在多个后端进行绘图。在进行有关绘图的选择时,我总是觉得此页面非常有用:http://docs.juliaplots.org/latest/backends/