# How to plot a simple stacked area chart

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [March 1, 2019, 3:35pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351 "2019-03-01T15:35:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [March 1, 2019, 3:57pm UTC](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351/2 "2019-03-01T15:57:46Z")

</div>

Here’s one way:

```julia
using Plots

@userplot StackedArea

# a simple "recipe" for Plots.jl to get stacked area plots
# usage: stackedarea(xvector, datamatrix, plotsoptions)
@recipe function f(pc::StackedArea)
    x, y = pc.args
    n = length(x)
    y = cumsum(y, dims=2)
    seriestype := :shape

    # create a filled polygon for each item
    for c=1:size(y,2)
        sx = vcat(x, reverse(x))
        sy = vcat(y[:,c], c==1 ? zeros(n) : reverse(y[:,c-1]))
        @series (sx, sy)
    end
end

a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
x = [2001,2002,2003,2004,2005,2006]

plotly()
stackedarea(x, [a b c], labels=reshape(sNames, (1,3)))

```

![stacked](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed4f5d86ef1e6d47a160bcdd9a58886cc1a59544.png)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-plot-a-simple-stacked-area-chart/21351)._
