3

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?

1
  • 2
    for your ranges and function, the syntax is simply: surface(x, y, f) Commented Mar 2, 2021 at 23:14

1 Answer 1

10

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)
Sign up to request clarification or add additional context in comments.

1 Comment

handy solution, but I just wanted to point out the hilariously unfortunate concatenation of 10 on the x-axis with 0 on the y-axis to produce something that very much looks like 100 lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.