# Stacked Plots (lines) Help

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

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

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

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

``````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.
https://docs.juliaplots.org/latest/gallery/gr/generated/gr-ref058/#gr_ref058

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.

One way using Interpolations.jl:

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

1 Like

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:

``````        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)
`````` 1 Like

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: