Unmatched dimensions for 3D plot

I have a program to solve 1D Burguer’s equation. I have the following values/dimensions, which are giving me a dimensional error. I don’t understand what is going wrong. If anyone can help me, I appreciate it.

julia> size(xs) 
(50,)  

julia> size(ts) 
(1000,)  

julia> size(u) 
(50, 1001)  

julia> plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U") 
Arrays have incorrect length or dimension.

More on the program itself, in case it helps

Parameters (space)

nx= 50;
delta_x = 15/(nx - 1)
x = range(0, stop=delta_x*(nx-1), length=nx) # Full range of spatial steps for wich a solution is desired

Parameters (time)

endTime = 2   # simulation end time
nt = 1000          # nt is the number of timesteps we want to calculate
delta_t = endTime/nt  # Δt is the amount of time each timestep covers
t = range(0, stop=endTime, length=nt) # Full range of time steps for which a solution is desired

#+RESULTS:
: 0.0:0.002002002002002002:2.0

Initial conditions (space-time)

# Init array of ones at initial timestep
u_zero = ones(nx) 
  
# Set u₀ = 2 in the interval 0.5 ≤ x ≤ 1 as per our I.C.s
u_zero[0.5 .<= x .<= 3] .= 2  # Note use of . (dot) broadcasting syntax
  
u_zero

#+RESULTS:
: [1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

Run the differential equation

# u[:,] = copy(u_zero) # Initialise arbitrary future timestep with inital condition, u_zero
u=zeros((nx,nt+1))
u[:,1]=copy(u_zero)
  
for n in 1:nt       # loop over timesteps, n: nt times
    u[:,n+1] = copy(u[:,n]) # copy the existing values of u^n into u^(n+1)
    for i in 2:nx   
        u[i,n+1] = u[i,n] - u[i,n] * delta_t/delta_x * (u[i,n] - u[i-1,n])
    end
end
using Plots
gr()
plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")

I think u should have size (1000,50) for that to work. (note that you have 1001, so that will be a problem as well).

I have specified to use only 1:1000 time-steps to plot. That does not solve it.

I have answered this question here:

https://stackoverflow.com/questions/70851419/im-having-trouble-generating-a-3d-surface-plot-in-julia-im-using-gr-backend/70852091#70852091

(and yes I think @lmiq is correct)

plot(xs,ts,u'[1:1000,1:50],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")
attempt to save state beyond implementation limit
send: Broken pipe
Error showing value of type Plots.Plot{Plots.GRBackend}:
ERROR: GR3 error (/workspace/srcdir/gr/lib/gr3/gr3.c, l. 1886): invalid value
Stacktrace:
  [1] _check_error()
    @ GR.GR3 ~/.julia/packages/GR/KPElO/src/gr3.jl:50
  [2] surface(px::Vector{Float64}, py::Vector{Float64}, pz::Matrix{Float64}, option::Int64)
    @ GR.GR3 ~/.julia/packages/GR/KPElO/src/gr3.jl:366
  [3] gr_draw_surface(series::Plots.Series, x::Vector{Float64}, y::Vector{Float64}, z::Matrix{Float64}, clims::Tu
ple{Float64, Float64})
    @ Plots ~/.julia/packages/Plots/9C6z9/src/backends/gr.jl:2043
  [4] gr_add_series(sp::Plots.Subplot{Plots.GRBackend}, series::Plots.Series)
    @ Plots ~/.julia/packages/Plots/9C6z9/src/backends/gr.jl:1851
(...)

By the way, I didn’t write how to obtain xs and ts,

xs = collect(x)
ts = collect(t)

I think that’s an unrelated error - I’ve seen that earlier when trying to plot a surface after closing the Gtk window that Plots/GR opens. Can you try restarting?

Yes, I had tried that.

Actually, @nilshg and @lmiq were right. But, further, my problem seem to be GR itself. For some reason, it’s not plotting surfaces. When I changed the back-end to pyplot(), then It worked fine.

1 Like

Glad you got it working - have you tried my example from StackOverflow with GR backend? GR should plot surfaces just fine

I don’t know why it’s either breaking or giving me spurious results.

Here is your example run in my computer:

See, the mash is shifted for some reason (wasn’t working beforehand).

EDIT: Also, it’s using the names I gave to the axis to the pyplot before this one.

Can you try again from a fresh Julia session?

It really seems like there’s a bug in Plots/GR at the moment, or my setup is somehow broken. Could someone else try to reproduce this?

julia> using Plots

julia> x = 1:5; y = 1:10; z = [x + y for x ∈ 1:5, y ∈ 1:10];

julia> surface(x, y, z') # fine 

# Close the Gtk window

julia> surface(x, y, z')
read: No error
Error showing value of type Plots.Plot{Plots.GRBackend}:
ERROR: GR3 error (/workspace/srcdir/gr/lib/gr3/gr3.c, l. 1886): invalid value

(@v1.7) pkg> st Plots
      Status `C:\Users\ngudat\.julia\environments\v1.7\Project.toml`
  [91a5bcdd] Plots v1.25.6

(@v1.7) pkg> st -m GR
      Status `C:\Users\ngudat\.julia\environments\v1.7\Manifest.toml`
  [28b8d3ca] GR v0.63.1

Yes, exactly the same thing happened, from a fresh Julia session.

julia> using Plots

julia> x = 1:5; y = 1:10; z = [x + y for x ∈ 1:5, y ∈ 1:10];

julia> surface(x, y, z')  #works

julia> surface(x, y, z') ## Indefinitely hang
^C^C^C^C^C^C^C^C^CWARNING: Force throwing a SIGINT
Error showing value of type Plots.Plot{Plots.GRBackend}:
(...)

I’m also with the same versions,

(@v1.7) pkg> st Plots
st -      Status `~/.julia/environments/v1.7/Project.toml`
  [91a5bcdd] Plots v1.25.6

(@v1.7) pkg> st -m GR
      Status `~/.julia/environments/v1.7/Manifest.toml`
  [28b8d3ca] GR v0.63.1

This seems to work in Plots.jl gr():

plot(x, t, u[:,1:1000]',st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")

Plots_gr_surface

1 Like

Okay, looks like a bug to me, opened an issue here: [BUG] `surface` errors when called after closing plot window · Issue #4070 · JuliaPlots/Plots.jl · GitHub

1 Like