# How to produce a waterfall plot in Julia?

**URL:** https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441
**Category:** Visualization
**Tags:** question, plotting
**Created:** [January 24, 2023, 3:41am UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441 "2023-01-24T03:41:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [January 24, 2023, 3:41am UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/1 "2023-01-24T03:41:45Z")

</div>

I want to produce a waterfall plot, sort of like this:

 ![Fig2_imported_stable_soliton_c=0.4_cropped](https://global.discourse-cdn.com/julialang/original/3X/8/4/848d91498037aa4b69877d899d8f9ed8aa27df12.png)

in Julia. The above plot was created in MATLAB with the command:

```matlab
waterfall(xi, tdata, abs(u1data'))

```

where xi and tdata was 1d arrays and u1data is a 2d array containing u1 at each xi and t value. Is there a corresponding way of creating such a plot in Julia?

If not, how does one produce a surface plot with just matrices (no function for finding z from x and y)? I’ve tried this:

```julia
using PlotlyJS;
PlotlyJS.plot(surface(z=abs.(u1data), x=xi, y=tdata));

```

and absolutely no plot is generated (nor is an error message or warning message). I’ve issued this command both in the Julia REPL and in Jupyter Lab. I have WebIO installed using the command:

```bash
sudo python3 -m pip3 install webio_jupyter_extension

```

(where Jupyter Lab itself is installed system-wide also).

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [January 24, 2023, 5:43am UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/2 "2023-01-24T05:43:42Z")

</div>

I’ve been able to answer the surface plot question of mine. I just converted xi and tdata to 2d arrays with each element corresponding to the value of the variable in question at the particular point in the grid, and used plot\_surface from PyPlot. Specifically, using the code:

```julia
using PyPlot;
pygui(true);
PyPlot.figure(1);
PyPlot.xlabel(L"$\xi$")
PyPlot.ylabel(L"$t$")
PyPlot.zlabel(L"$|u|$")
PyPlot.plot_surface(ones(length(tdata))' .* xi, tdata' .* ones(length(xi)), abs.(u1datar))

PyPlot.figure(2);
PyPlot.xlabel(L"$\xi$")
PyPlot.ylabel(L"$t$")
PyPlot.zlabel(L"$|u|$")
PyPlot.plot_surface(ones(length(tdata))' .* xi, tdata' .* ones(length(xi)), abs.(u2datar))

```

pygui(true) makes the plot get launched as an interactive GUI (as opposed to a static image in the browser, which is the default); the xlabel, ylabel and zlabel commands are just adding in labels.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [January 24, 2023, 10:01am UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/3 "2023-01-24T10:01:03Z")

</div>

Here’s something I came up with using Makie:

```julia
using CairoMakie

xi = -15:0.1:15
t = 0:30
u1data = [exp(-(x-0.5*(t-15))^2) for x in xi, t in t]

fig = Figure()
ax = Axis3(fig[1,1];
    azimuth=-1.46, elevation=1,
    xticks=-15:5:15, zticks=0:2,
    limits=(nothing, nothing, (0,2)),
    xgridvisible=false, ygridvisible=false, zgridvisible=false,
    (Symbol.([:x, :y, :z], "spinecolor_", [2 3]) .=> :transparent)...,
    xlabel=L"\xi", ylabel=L"t", zlabel=L"|u|")

for (tval, z) in Iterators.reverse(zip(t, eachcol(u1data)))
    tvals = fill(tval, length(xi))
    band!(Point3.(xi, tvals, 0), Point3.(xi, tvals, z), color=:white)
    lines!(xi, tvals, z, color=:black)
end

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a43f3d91ae0223396ae57c52da88e0909e6ad57.png)

(Use `GLMakie` or `WGLMakie` instead of `CairoMakie` for interactivity.)

> [@fusion809](#):
>
> how does one produce a surface plot with just matrices (no function for finding z from x and y)?

With Makie you can do `surface(xi, t, u1data)` where `xi` and `t` are vectors and `u1data` is a matrix.

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [January 24, 2023, 11:22am UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/5 "2023-01-24T11:22:30Z")

</div>

I would like to just say thank you for the obvious effort you put into your answer, it is very much appreciated. My tdata is not equally spaced and it has over 49,000 elements, so I had to adapt your code to my situation. Namely, I had to write code to cherry pick elements of tdata that were closest to the integer values of t. With this change, however, I find your code with WGLMackie did produce the required plot window.

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [January 24, 2023, 12:15pm UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/6 "2023-01-24T12:15:45Z")

</div>

I do have a follow up query though. How do you save this figure to a file? I’ve looked up the documentation for Makie but its save() function only seems to work when you feed it a scene object and there’s no scene object created in this code.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [January 24, 2023, 12:20pm UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/7 "2023-01-24T12:20:12Z")

</div>

That must have been old docs, you can save `Figure`, `Scene` or `FigureAxisPlot` with `save`.

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [January 24, 2023, 12:22pm UTC](https://discourse.julialang.org/t/how-to-produce-a-waterfall-plot-in-julia/93441/8 "2023-01-24T12:22:28Z")

</div>

Thanks, I had just found [Exporting a Figure with physical dimensions](https://docs.makie.org/stable/documentation/figure_size/) when you wrote that reply and it also mentions the option of saving a Figure object.
