Plotly gives error with contour plot

I am using Julia 1.4.1 with juno. I want to draw a filled contour plot. Following codes give me error.


using Plots

x = -3:0.1:3
y = -3:0.1:3
X = reshape([xi for xi in x for yj in y],  length(y), length(x))
Y = reshape([yj for xi in x for yj in y],  length(y), length(x))


Z = X + im*Y;
R(z)= @. (1 + z + 1/2*z^2 + 1/6*z^3 + 1/24*z^4);
R3 = abs.(R(Z));

plotly()
levs=[0; 1]
contour(x, y, R3,levels=levs,fill=true) 
#contour(x, y, R3,levels=levs)

Error:

MethodError: no method matching +(::Array{Int64,1}, ::Int64)

Closest candidates are:

+(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529

+(!Matched::Complex{Bool}, ::Real) at complex.jl:301

+(!Matched::Missing, ::Number) at missing.jl:115

...

plotly_series(::Plots.Plot{Plots.PlotlyBackend}, ::Plots.Series) at plotly.jl:520

plotly_series(::Plots.Plot{Plots.PlotlyBackend}) at plotly.jl:797

plotly_series_json at plotly.jl:803 [inlined]

plotly_html_body(::Plots.Plot{Plots.PlotlyBackend}, ::Nothing) at plotly.jl:838

plotly_html_body at plotly.jl:833 [inlined]

html_body at plotly.jl:808 [inlined]

standalone_html(::Plots.Plot{Plots.PlotlyBackend}; title::String) at web.jl:7

standalone_html(::Plots.Plot{Plots.PlotlyBackend}) at web.jl:7

_show at plotly.jl:874 [inlined]

_showjuno at output.jl:284 [inlined]

showjuno(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("text/html")}, ::Plots.Plot{Plots.PlotlyBackend}) at output.jl:266

show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("application/prs.juno.plotpane+html")}, ::Plots.Plot{Plots.PlotlyBackend}) at output.jl:221

show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Plots.Plot{Plots.PlotlyBackend}) at multimedia.jl:109

displayinplotpane(::Plots.Plot{Plots.PlotlyBackend}) at showdisplay.jl:51

displayandrender(::Plots.Plot{Plots.PlotlyBackend}) at showdisplay.jl:131

(::Atom.var"#201#205")() at eval.jl:177

#invokelatest#1 at essentials.jl:712 [inlined]

invokelatest at essentials.jl:711 [inlined]

(::Atom.var"#200#204"{String,String})() at eval.jl:176

withpath(::Atom.var"#200#204"{String,String}, ::String) at utils.jl:30

withpath(::Function, ::String) at eval.jl:9

#199 at eval.jl:161 [inlined]

with_logstate(::Atom.var"#199#203"{String,String}, ::Base.CoreLogging.LogState) at logging.jl:398

with_logger at logging.jl:505 [inlined]

#198 at eval.jl:160 [inlined]

hideprompt(::Atom.var"#198#202"{String,String}) at repl.jl:140

macro expansion at dynamic.jl:24 [inlined]

evalall(::String, ::Nothing, ::String) at eval.jl:150

macro expansion at eval.jl:39 [inlined]

(::Atom.var"#172#173")() at task.jl:358

If I use a scalar value instead of levs say 5 then it works however it does not work with array levs. Also, If I use gr() backend contour(x, y, R3,levels=levs) works but contour(x, y, R3,levels=levs,fill=true) does not work. How can I fix these errors?

My guess is that the error comes from this set:

R(z)= @. (1 + z + 1/2*z^2 + 1/6*z^3 + 1/24*z^4);
R3 = abs.(R(Z));

Instead, I would change it to

R(z) = 1 + z + 1/2*z^2 + 1/6*z^3 + 1/24*z^4
R3 = abs.(R.(Z));

This correctly broadcasts the function over the matrix.

Thank you for your suggestion. But, it does not work. I get same error.

Maybe specifying contour levels is not supported with the plotly backend and Plots.jl? But you can get something working with Plotlyjs.jl directly. If you only want two levels, 0 or 1, you could use the following:

using PlotlyJS, ORCA

x = -3:0.1:3
y = -3:0.1:3
X = reshape([xi for xi in x for yj in y],  length(y), length(x))
Y = reshape([yj for xi in x for yj in y],  length(y), length(x))

Z = X + im*Y;
R(z)= @. (1 + z + 1/2*z^2 + 1/6*z^3 + 1/24*z^4);
R3 = abs.(R(Z))

testcontour = Plot(contour(; x=x, y=y, z=R3, autocontour=false, contours=Dict(:start=>0, :end=>1, :size=>1)))
ORCA.savefig(testcontour, "testcontour.png")
ORCA.savefig(testcontour, "testcontour.html")

Which yields the following:

1 Like