Problems with surface

Dear all,
I want to plot a simple surface, as in the following command:


using Plots
gr()
xs = collect(0.1:0.05:2.0)
ys = collect(0.2:0.1:2.0)
X = [x for x = xs for _ = ys]
Y = [y for _ = xs for y = ys]
Z = ((x, y)->begin
            1 / x + y * x ^ 2
     end)

surface(X, Y, Z.(X, Y), xlabel = "longer xlabel", ylabel = "longer ylabel", zlabel = "longer zlabel")

The problem is that the result does not appear the produced surface.
What’s the problem?
Anyone can help me please?

These are my packages, at Julia 1.11.0-beta2

julia> Pkg.status()
Status `C:\Users\Angelo\.julia\environments\v1.11\Project.toml`
  [a076750e] CPLEX v1.0.3
⌃ [a93c6f00] DataFrames v1.6.1
⌃ [5789e2e9] FileIO v1.16.3
⌃ [f6369f11] ForwardDiff v0.10.36
⌃ [2e9cd046] Gurobi v1.3.0
⌃ [7073ff75] IJulia v1.25.0
⌃ [033835bb] JLD2 v0.4.48
⌃ [4076af6c] JuMP v1.22.2
  [bcdb8e00] Metaheuristics v3.3.5
  [a03496cd] PlotlyBase v0.8.19
  [f2990250] PlotlyKaleido v2.2.5
⌃ [91a5bcdd] Plots v1.40.4
  [10745b16] Statistics v1.11.1
  [37e2e46d] LinearAlgebra v1.11.0
  [de0858da] Printf v1.11.0
  [9a3f8284] Random v1.11.0

A simple way to plot this is to pass the function directly to surface:

xs = 0.1:0.05:2.0
ys = 0.2:0.1:2.0
z(x, y) = 1/x + y*x^2
surface(xs, ys, z)

Not work yet, I have run your code. Result:

Your code works for me (which maybe isn’t surprising given that it’s just the docs example for surface).

I note that you are on a beta version of Julia 1.11, and have an outdated version of Plots.jl installed as well. I would recommend you update both Julia and Plots to the latest stable releases.

2 Likes