# Unmatched dimensions for 3D plot

**URL:** https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180
**Category:** General Usage
**Tags:** plotting, diffeq, plots, gr
**Created:** [January 25, 2022, 3:46pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180 "2022-01-25T15:46:07Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 3:46pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/1 "2022-01-25T15:46:07Z")

</div>

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
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)

```julia
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)

```julia
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)

```julia
# 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

```julia
# 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

```

```julia
using Plots
gr()

```

```julia
plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 25, 2022, 4:02pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/2 "2022-01-25T16:02:11Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 4:24pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/3 "2022-01-25T16:24:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 4:25pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/4 "2022-01-25T16:25:06Z")

</div>

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](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)

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 4:34pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/5 "2022-01-25T16:34:18Z")

</div>

```julia
plot(xs,ts,u'[1:1000,1:50],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")

```

```julia
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
(...)

```

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 4:38pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/6 "2022-01-25T16:38:51Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 4:58pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/7 "2022-01-25T16:58:39Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 5:05pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/9 "2022-01-25T17:05:05Z")

</div>

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.

 ![pic-selected-220125-1400-32](https://global.discourse-cdn.com/julialang/original/3X/b/9/b9f0acdcd757fb7e7b1af4c55eda2f4fc230310e.png)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 5:06pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/10 "2022-01-25T17:06:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 5:08pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/11 "2022-01-25T17:08:26Z")

</div>

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

Here is your example run in my computer:

 ![pic-selected-220125-1407-08](https://global.discourse-cdn.com/julialang/original/3X/7/9/794772d23b6886cd30e81c501a93d800b1b5fa71.png)

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.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 5:10pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/12 "2022-01-25T17:10:00Z")

</div>

Can you try again from a fresh Julia session?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 5:13pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/13 "2022-01-25T17:13:04Z")

</div>

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
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

```

---

<div class="post-metadata">

### Author: ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)
#### Post date: [January 25, 2022, 5:36pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/14 "2022-01-25T17:36:50Z")

</div>

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

```julia
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,

```julia
(@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

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 25, 2022, 5:41pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/15 "2022-01-25T17:41:25Z")

</div>

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

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

```

![Plots_gr_surface](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc76a155a1c81e99826532f5a09a61a13c0883c5.png)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 25, 2022, 8:48pm UTC](https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/16 "2022-01-25T20:48:00Z")

</div>

Okay, looks like a bug to me, opened an issue here: [https://github.com/JuliaPlots/Plots.jl/issues/4070](https://github.com/JuliaPlots/Plots.jl/issues/4070)
