# Fast plotting of area-filled signals with interactive zoom

**URL:** <https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611>\
**Category:** Visualization\
**Tags:** plotting, plots, makie, gmt\
**Created:** [June 26, 2021, 6:08pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611 "2021-06-26T18:08:53Z")\
**Posts on this page:** 9\
**Page:** 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:** [June 26, 2021, 6:08pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/1 "2021-06-26T18:08:53Z")

</div>

Plotting 50 signals below with filled areas using PyPlot.jl takes ~0.2 s (after first plot) and allows fast interactive zoom-in:

 ![PyPlot_Qt5_signal_trace_filled_are](https://global.discourse-cdn.com/julialang/original/3X/b/b/bbc5dfb76132eae7024a3ba1192cb1c7059d1a49.png)  
Interactive zoom-in:  
 ![PyPlot_Qt5_signal_trace_filled_area_ZOOM](https://global.discourse-cdn.com/julialang/original/3X/0/c/0ca8c1302c513ab59e2c94371dfe34e8365bbc75.png)

A MWE is provided herein:

```julia
using PyCall
pygui(:qt5)
using PyPlot

# Define function of some oscillatory signal:
f(t,t0) = (t>t0) ? sin(2π*20*(t - t0)) * exp(-10*(t - t0)) : 0

# Create input signal data:
t = 0:0.001:1.0
n = length(t)
Ny = 50
yyt = fill(0.0,Ny,n)
for i in 1:Ny
    yyt[i,:] = f.(t, 0.7*i/Ny)
end

# Plot signals with positive/negative areas filled:
@time begin
  for i in 1:Ny
      PyPlot.fill_between(t, yyt[i,:] .+ i, i, where=(yyt[i,:] .< 0), alpha=0.3, color="red", interpolate=true)
      PyPlot.fill_between(t, yyt[i,:] .+ i, i, where=(yyt[i,:] .>= 0), alpha=0.3, color="blue", interpolate=true)
      PyPlot.plot(t, yyt[i,:] .+ i, lw=0.4, color=:black)
      PyPlot.plt.autoscale(enable=true, tight=true)
  end
  PyPlot.tight_layout()
end # ~ 0.20 seconds

```

Does anybody know if there is a better or equivalent interactive solution for this type of filled-area plots using Makie, Plots.jl or other?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 26, 2021, 7:48pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/2 "2021-06-26T19:48:09Z")

</div>

Hmm, are you after plotting seismic traces? GMT has [this old segy module](https://docs.generic-mapping-tools.org/latest/supplements/segy/segy.html) that I never used nor ported to GMT.jl (but it can still be used with the _monolithic_ mode).

The `wiggle` module also seems appropriated to what you are looking for. See [this image](https://docs.generic-mapping-tools.org/latest/_images/ex09.png)

---

<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:** [June 26, 2021, 8:09pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/3 "2021-06-26T20:09:25Z")

</div>

Yes, it’s for displaying acoustic signals.  
The GMT image you shared is absolutely brilliant!  
As far as the “monolithic” solution goes, it doesn’t sound very interactive?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 26, 2021, 8:17pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/4 "2021-06-26T20:17:22Z")

</div>

Yes, one can make very nice patterns with magnetic anomalies.

The [“monolithic”](https://www.generic-mapping-tools.org/GMT.jl/dev/monolitic/#Monolithic) mode designates the die-hard pure GMT syntax and can be used by any module. It’s actually a bit faster because it skips a lot of parsing code but the end result is exactly the same. GMT modules (almost all in the _supplements_) that have not yet been ported to GMT.jl can still be used in Julia using that terse syntax.

If you select the `pdf` output then you can zoom in a lot because figure is vectorial in origin (postscript) and that is ofc preserved in conversion.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [June 28, 2021, 9:40am UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/5 "2021-06-28T09:40:48Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/6/7/67feed51a814cb89f677c22c561f338e49dd9d42.png)

```julia
using GLMakie
# Define function of some oscillatory signal:
f(t,t0) = (t>t0) ? sin(2π*20*(t - t0)) * exp(-10*(t - t0)) : 0

# Create input signal data:
t = 0:0.001:1.0
n = length(t)
Ny = 50
yyt = fill(0.0,Ny,n)
for i in 1:Ny
    yyt[i,:] = f.(t, 0.7*i/Ny)
end

# Plot signals with positive/negative areas filled:

@time begin
    fig = Figure(resolution=(600, 1000))
    ax = Axis(fig[1,1])
    for i in 1:Ny
        band!(ax, t, map(x-> x < 0 ? x + i : i, yyt[i,:]), i, color=("red", 0.3))
        band!(t, map(x-> x >= 0 ? x + i : i, yyt[i,:]), i, color=("blue", 0.3))
        lines!(ax, t, yyt[i,:] .+ i, linewidth=0.4, color=:black)
    end
    tightlimits!(ax)
    display(fig)
end # ~ 0.70 seconds

```

It takes a surprising long time to build the plot (0.7s), but drawing is instant, so interactivity is smooth.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [June 28, 2021, 10:20am UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/6 "2021-06-28T10:20:29Z")

</div>

You can also do the 3d thingy like GMT with Makie:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d7d79f08b6293dd34f24bcda941a85ae5770d22.png)

Although it may be less elegant, and not very well tested (found two bugs in the limits with translation 😅, that must be worked around by setting the limits manually ):

```julia

fig = Figure(resolution=(1000, 1000))
ax = Axis3(fig[1,1])
ylims!(ax, 0, Ny)
for i in 1:Ny
    yyti = yyt[i,:] ./ 10
    trans = Makie.Transformation(ax.scene)
    translate!(trans, 0, i, 0)
    rotate!(trans, Vec3f0(1, 0, 0), 0.2pi)
    band!(ax, t, map(x-> x < 0 ? x : 0, yyti), 0, color=("red", 0.3), transformation=trans, transparency=true)
    band!(t, map(x-> x >= 0 ? x : 0, yyti), 0, color=("blue", 0.3), transformation=trans, transparency=true)
    lines!(ax, t, yyti, linewidth=0.4, color=:black, transformation=trans, transparency=true)
end
display(fig)

```

---

<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:** [June 28, 2021, 12:13pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/7 "2021-06-28T12:13:49Z")

</div>

@sdanisch, thank you very much indeed for your brilliant Makie solution. This is one of the joys of Julia.

Makie output is excellent in terms of display quality and interactivity. The 3D rendering example was awesome too 🙂

The time required to build the plot in my Win10 laptop is about 2 s, or ~10x slower than PyPlot. That is still reasonable and will mark it as a solution.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [June 28, 2021, 12:24pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/8 "2021-06-28T12:24:36Z")

</div>

> The time required to build the plot in my Win10 laptop is about 2 s, or ~10x slower than PyPlot.

We should profile why… Pretty sure that Makie should be able to be at least as fast as Pyplot for constructing the plot.

---

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [May 30, 2023, 7:03pm UTC](https://discourse.julialang.org/t/fast-plotting-of-area-filled-signals-with-interactive-zoom/63611/9 "2023-05-30T19:03:47Z")

</div>

Sorry to revive an old post, but it is better than double. Is it possible to get the same figure with the axis interchanged. I mean, the signals in vertical instead of horizontal?
