Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Spheres in same figure #323

Closed
ZenanH opened this issue May 4, 2020 · 2 comments
Closed

Multiple Spheres in same figure #323

ZenanH opened this issue May 4, 2020 · 2 comments

Comments

@ZenanH
Copy link

ZenanH commented May 4, 2020

I want to draw a number of spheres at different locations in the same figure, each as a trace. Here is my code:

using PlotlyJS

function sphere(sphere_number::Int64)

    trace = []

    for i = 1:sphere_number
        d = rand(1:50)
        n = 100
        θ = range(0.0, 2*π, length = n)
        ϕ = range(0.0, π, length = n)

        a = d .+ cos.(θ)*sin.(ϕ)'
        b = d .+ sin.(θ)*sin.(ϕ)'
        c = d .+ ones(n)*cos.(ϕ)'

        push!(trace,surface(x = a, y = b, z = c, colorscale="Greys"))
    end

    layout = Layout(title="Sphere", autosize=false, width=400,
                    height=400, margin=attr(l=65, r=50, b=65, t=90))
    plot(trace, layout)
end

Error:

ERROR: MethodError: no method matching Plot(::Array{Any,1}, ::Layout{Dict{Symbol,Any}})
Closest candidates are:
  Plot(::Array{TT,1}, ::PlotlyBase.AbstractLayout, ::Base.UUID, ::Style) where TT<:AbstractTrace at C:\Users\zenan\.julia\packages\PlotlyBase\80KwD\src\PlotlyBase.jl:33
  Plot(::AbstractArray{T,1}, ::Any; style) where T<:AbstractTrace at C:\Users\zenan\.julia\packages\PlotlyBase\80KwD\src\PlotlyBase.jl:58
  Plot(::AbstractArray{T,N} where N, ::Layout; kwargs...) where T<:Union{Dates.Date, Dates.DateTime, AbstractString, Number, Symbol} at C:\Users\zenan\.julia\packages\PlotlyBase\80KwD\src\convenience_api.jl:61
  ...
Stacktrace:
 [1] plot(::Array{Any,1}, ::Vararg{Any,N} where N; options::Dict{Any,Any}, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at 
C:\Users\zenan\.julia\packages\PlotlyJS\AhkM5\src\display.jl:145
 [2] plot(::Array{Any,1}, ::Vararg{Any,N} where N) at C:\Users\zenan\.julia\packages\PlotlyJS\AhkM5\src\display.jl:145
 [3] sphere(::Int64) at e:\GitHub\TEMP.jl:19
 [4] top-level scope at REPL[38]:1
 [5] eval(::Module, ::Any) at .\boot.jl:331
 [6] eval_user_input(::Any, ::REPL.REPLBackend) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:86
 [7] run_backend(::REPL.REPLBackend) at C:\Users\zenan\.julia\packages\Revise\MgvIv\src\Revise.jl:1023
 [8] top-level scope at none:0
 [9] eval(::Module, ::Any) at .\boot.jl:331
 [10] eval_user_input(::Any, ::REPL.REPLBackend) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:86
 [11] run_backend(::REPL.REPLBackend) at C:\Users\zenan\.julia\packages\Revise\MgvIv\src\Revise.jl:1023
 [12] top-level scope at none:0

I did this because I noticed the way plot([trace1,trace2], layout) was used in the doc.
How can I solve this problem?😅

@sglyon
Copy link
Member

sglyon commented May 4, 2020

This is close!

I think the problem here is that the trace = [] gives you an Array{Any,1} and the plot method needs an Array{<:AbstractTrace,1}

Can you try again and start with trace = AbstractTrace[]?

@ZenanH
Copy link
Author

ZenanH commented May 5, 2020

Thank you very much, it worked!👍

@ZenanH ZenanH closed this as completed May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants