# Stacked Plots (lines) Help

**URL:** https://discourse.julialang.org/t/stacked-plots-lines-help/95576
**Category:** Visualization
**Tags:** question, plots
**Created:** [March 5, 2023, 1:13pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576 "2023-03-05T13:13:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rodolforbcoutinho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodolforbcoutinho/32/27067_2.png) [@rodolforbcoutinho](https://discourse.julialang.org/u/rodolforbcoutinho)
#### Post date: [March 5, 2023, 1:13pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/1 "2023-03-05T13:13:38Z")

</div>

Hi all!  
I’m trying to make a stacked plot from the Data Below

when I plot using the regular plot function, i got the correct values/positions, but i need then stacked. Using areaplot instead of plot is not working as expected.

Anyone could tell what I’m doing wrong???  
Thanks in advance!!!

```julia
x = [0 0 0; 1 3 5; 4 8 7; 10 10 10]
y = [0 0 0; 7 8 9; 0 0 0; 0 0 0]
plot(x,y, seriescolor = [:red :green :blue], seriestype = :steppost) #g1
areaplot(x,y, seriescolor = [:red :green :blue], fillalpha = 0.3, seriestype = :steppost) #g2

```

 ![Plot X Areaplot](https://global.discourse-cdn.com/julialang/original/3X/b/5/b50763fdb1c2f169c73be65961ffcbb67a52ceba.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: [March 5, 2023, 4:48pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/2 "2023-03-05T16:48:58Z")

</div>

For stacked area plots, I think we need multiple y-series to be plotted against a common x-axis support. Each series is a column of the `y`-matrix and should have same length as the support vector `x`.

---

<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: [March 6, 2023, 12:44pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/3 "2023-03-06T12:44:47Z")

</div>

Fyi, a workaround to get what you want would be to superpose the individual area plots:

```julia
x = [0 0 0; 1 3 5; 4 8 7; 10 10 10]
y = [0 0 0; 7 8 9; 0 0 0; 0 0 0]

colors = [:red :green :blue]
p = plot()
[areaplot!(x, y, c=c, fa=0.3, st=:steppost) for (x,y,c) in zip(eachcol(x),eachcol(y),colors)]
p

```

 ![Plots_area_plots_superposed](https://global.discourse-cdn.com/julialang/original/3X/8/d/8db11ee9c8e1341c8e1fb51625408ba4111d7962.png)

---

<div class="post-metadata">

### Author: ![rodolforbcoutinho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodolforbcoutinho/32/27067_2.png) [@rodolforbcoutinho](https://discourse.julialang.org/u/rodolforbcoutinho)
#### Post date: [March 6, 2023, 6:12pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/4 "2023-03-06T18:12:51Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> x = [0 0 0; 1 3 5; 4 8 7; 10 10 10]
> y = [0 0 0; 7 8 9; 0 0 0; 0 0 0]
> 
> colors = [:red :green :blue]
> p = plot()
> [areaplot!(x, y, c=c, fa=0.3, st=:steppost) for (x,y,c) in zip(eachcol(x),eachcol(y),colors)]
> p
> 
> ```

Hi Rafael, thanks for helping here!

Maybe i wasnt clear about what i was trying to do. I would like a plot more like the one in link bellow from julia plot documentation, stacked. The shaded area are a great visual but not needed here.

For example, at x = 3 red curve has value 7 and the green one has value 8, it should be 15 in the graphic 7(red) + 8(green). At x = 4, the plot should back to 8 value as red function “has ended” but green is still on.

I could recreate this 3 curves with shared/sync x axis but it will cost memory allocation. The original data had lenght of 288, for each curve. Then i found the attribute “seriestype = :steppost” and it lowered from 288 to 4. The end application should be embedded somewhere, so less memory less final cost.

> **[Stacked area chart · Plots](https://docs.juliaplots.org/latest/gallery/gr/generated/gr-ref058/#gr_ref058)**
>
> Documentation for Plots.

---

<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: [March 6, 2023, 6:25pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/5 "2023-03-06T18:25:49Z")

</div>

As per my first reply, for stacked area plots, all series need to have the same support along the x-axis, which is not the case with your input data.

The simplest thing to do is perhaps to interpolate all series at all distinct x-axis values.

---

<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: [March 6, 2023, 7:14pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/6 "2023-03-06T19:14:00Z")

</div>

One way using Interpolations.jl:

```julia
using Interpolations, Plots

x = Float64[0 0 0; 1 3 5; 4 8 7; 10 10 10]
y = Float64[0 0 0; 7 8 9; 0 0 0; 0 0 0]

x0 = sort(unique(x))
ns = size(y,2)
y0 = zeros(length(x0), ns)
for (i,xi,yi) in zip(1:ns, eachcol(x), eachcol(y))
    itp = LinearInterpolation(xi, yi)
    y0[:,i] .= itp(x0)
end

areaplot(x0, y0)

```

 ![Plots_areaplot_interpolated_and_stacked](https://global.discourse-cdn.com/julialang/original/3X/8/6/867d571d8d2257937b89d748390cbc399a15d12a.png)

---

<div class="post-metadata">

### Author: ![rodolforbcoutinho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodolforbcoutinho/32/27067_2.png) [@rodolforbcoutinho](https://discourse.julialang.org/u/rodolforbcoutinho)
#### Post date: [March 7, 2023, 11:43am UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/7 "2023-03-07T11:43:51Z")

</div>

Nice example! I could figure out how to do the right stack plot. Didn’t have to use Interpolations.jl at all, and final result (x0, y0) are smaller than original problem.

Final code:

```julia
        x = [0 0 0; 1 3 5; 4 8 7; 10 10 10]
        y = [7 8 9]

        x0 = sort(unique(x))
        y0 = zeros(length(x0), length(y))

        for j in eachindex(y)
            for i in eachindex(x0)
                if x0[i] ≥ x[2,j] && x0[i] < x[3,j]
                    y0[i,j] = y[j]
                end
            end
        end

        p =areaplot(x0, y0, fa=0.3, st=:steppost, seriescolor = [:red :green :blue])
        display("image/png", p)

```

![Stacked_Plot_Final](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a7a2470dd8cd8125f269023f9b79b6eba3c7838.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: [March 7, 2023, 12:23pm UTC](https://discourse.julialang.org/t/stacked-plots-lines-help/95576/8 "2023-03-07T12:23:26Z")

</div>

To get more accurate answers, it helps to define the problem in the same way.

From the input data provided, hey, it’s a free land:

 ![input_data_plot](https://global.discourse-cdn.com/julialang/original/3X/a/d/adf2082b854ddf34321806072d132204f5f61406.png)
