Mohammad Saad
Mohammad Saad

Reputation: 2005

3D surface plot in julia

I would like to know how may I plot the data from following code as 3D-surface?

using Plots
function f(x)
    x1=x[1]
    x2=x[2]
    sin(x[1]) + cos(x[2])
end
#Sampling
function sam()
    x = range(0, 10.0, length = 9) |> collect
    y = range(0, 10.0, length = 9) |> collect
    tuple = zip(x,y) |> collect
    return tuple
end
xy = sam()
z = f.(xy)
plot(getindex.(xy,1),getindex.(xy,2),z)

I have tried using st=:surface in the plots() function with both gr() and pyplot() as backend but it doesn't work. May I know how can i plot this as surface within x,y,z limits?

Upvotes: 3

Views: 11336

Answers (1)

Benoit Pasquier
Benoit Pasquier

Reputation: 3015

It looks like you want to do

julia> using Plots

julia> f(x, y) = sin(x) + cos(y)
f (generic function with 1 method)

julia> surface(0:0.1:10, 0:0.1:10, f)

which gives

enter image description here

If you want to explicitly build the grid, you could do

julia> x = y = 0:0.1:10
0.0:0.1:10.0

julia> z = f.(x', y) ; # note the ' which permutes the dims of x

julia> surface(x, y, z)

Upvotes: 10

Related Questions