PlotlyJS Example3


Using API via Plots.jl

Passing JSON which has layout and data object

Example 1

using Plots
using JSON
plotlyjs()

p=plot(rand(15))

fdplotly(
    JSON.json((
        layout = Plots.plotly_layout(p),
        data = Plots.plotly_series(p),
    )); style = "width:300px;height:200x",
)

Example 2

using Plots
plotlyjs()

r = 2.0
N = 20

φ = range(0.0, 2π, length = N)
p = plot()
for θ in range(0, 2π, length = N)
    x = @. r * sin(θ) * cos(φ)
    y = @. r * sin(θ) * sin(φ)
    z = repeat([r * cos(θ)], N)
    scatter!(p, x, y, z, label = false, color = :blue)
end

fdplotly(JSON.json((
    layout = Plots.plotly_layout(p),
    data = Plots.plotly_series(p),
)); style = "width:400px;height:300x",
)

Using custom function

By using Plots.js_body function, we can mimic what Franklin.fdplotly does:

using Random
function fdplotly(p::Plots.Plot;
    id="fdp" * Random.randstring('a':'z', 3),
    style = "width:600px;height:350px")::Nothing
    b = Plots.js_body(p, id)
    return nothing
end
using Plots
plotlyjs()
p = plot(rand(10))
fdplotly(p; style = "width:400px;height:300x")
using Plots
plotlyjs()

r = 3.0
N = 20

φ = range(0.0, 2π, length = N)
p = plot()
for θ in range(0, 2π, length = N)
    x = @. r * sin(θ) * cos(φ)
    y = @. r * sin(θ) * sin(φ)
    z = repeat([r * cos(θ)], N)
    scatter!(p, x, y, z, label = false, color = :green)
end

fdplotly(p; style = "width:400px;height:300x")

Example: Torus

using Plots
plotlyjs()

a=3.

p = plot3d()
N = 45
u = range(0,2π,length=N)
for t in range(0,2π,length=N)
    x = @. cos(t) * (a + cos(u))
    y = @. sin(t) * (a + cos(u))
    z = @. sin(u)
    plot3d!(p, x, y, z, color=:blue, legend=false)
end

t=range(0,2π,length=N)
for u in range(0,2π,length=N)
    x = @. cos(t) * (a + cos(u))
    y = @. sin(t) * (a + cos(u))
    z = sin(u) * ones(length(x))
    plot3d!(p, x, y, z, color=:blue, legend=false)
end

fdplotly(p)