JuliaPy / PyPlot.jl

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

Unable to show legend in surface plot #350

Open rcalxrc08 opened 6 years ago

rcalxrc08 commented 6 years ago

Hi all,

i am getting the following error when i try to add a legend to an existing plot; the code is the following: using PyPlot; x=collect(linspace(0,1,100)); y=collect(linspace(0,2,20)); f(x,y)=sin.(x).*cos.(y); g(x,y)=sin.(x).*sin.(y); z1=[f(x_i,y_j) for x_i in x, y_j in y] z2=[g(x_i,y_j) for x_i in x, y_j in y] surf(x,y,z1') surf(x,y,z2') legend(["Function1","Function2"])

When i try to execute this code in the julia REPL i have no error and the legend shows up; image

but when i save the code in a script file and try to execute that through include("filename.jl") i am getting the following error and the legend does not show up:

ERROR: LoadError: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'AttributeError'> AttributeError("'Poly3DCollection' object has no attribute 'edgecolors2d'",) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 3744, in legend ret = gca().legend(*args, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes_axes.py", line 500, in legend self.legend = mlegend.Legend(self, handles, labels, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend.py", line 697, in init self._init_legend_box(handles, labels, markerfirst) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend.py", line 957, in _init_legend_box fontsize, handlebox)) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend_handler.py", line 119, in legend_artist fontsize, handlebox.get_transform()) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend_handler.py", line 733, in create_artists self.update_prop(p, orig_handle, legend) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend_handler.py", line 76, in update_prop self._update_prop(legend_handle, orig_handle) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\legend_handler.py", line 716, in _update_prop orig_handle.get_edgecolor()) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 706, in get_edgecolors return self._edgecolors2d

Stacktrace: [1] pyerr_check at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src\exception.jl:56 [inlined] [2] pyerr_check at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src\exception.jl:61 [inlined] [3] macro expansion at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src\exception.jl:81 [inlined] [4] #_pycall#67(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::Array{String,1}, ::Vararg{Array{String,1},N} where N) at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src\PyCall.jl:653 [5] _pycall(::PyCall.PyObject, ::Array{String,1}, ::Vararg{Array{String,1},N} where N) at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src\PyCall.jl:641 [6] #pycall#71(::Array{Any,1}, ::Function, ::PyCall.PyObject, ::Type{PyCall.PyAny}, ::Array{String,1}, ::Vararg{Array{String,1},N} where N) at C:\JuliaPro-0.6. 2.2\pkgs-0.6.2.2\v0.6\PyCall\src\PyCall.jl:675 [7] pycall(::PyCall.PyObject, ::Type{PyCall.PyAny}, ::Array{String,1}, ::Vararg{Array{String,1},N} where N) at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyCall\src \PyCall.jl:675 [8] #legend#72(::Array{Any,1}, ::Function, ::Array{String,1}, ::Vararg{Array{St ring,1},N} where N) at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyPlot\src\PyPlot.jl:172 [9] legend(::Array{String,1}, ::Vararg{Array{String,1},N} where N) at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\PyPlot\src\PyPlot.jl:169 [10] include_from_node1(::String) at .\loading.jl:576 [11] include(::String) at .\sysimg.jl:14 while loading C:\Users\Administrator\Desktop\JuliaProjects\BrokenPyPlot.jl, in expression starting on line 13`

Do you have any idea? Am I making some mistakes?