Search code examples
plotjuliastacked-area-chart

How can you make a stacked area / line chart in Julia with Plots.jl?


I would like to create a stacked area chart, similar to this for example, in Julia using Plots.

I know / suppose that you can do this if you directly use the Gadfly or PyPlot backends in Julia, but I was wondering if there was a recipe for this. If not, how can you contribute to the Plots Recipes? Would be a useful addition.


Solution

  • There's a recipe for something similar in

    https://docs.juliaplots.org/latest/examples/pgfplots/#portfolio-composition-maps

    For some reason the thumbnail looks broken now though (but the code works).

    The exact plot in the matlab example can be produced by

    plot(cumsum(Y, dims = 2)[:,end:-1:1], fill = 0, lc = :black)
    

    As a recipe that would look like

    @userplot AreaChart
    @recipe function f(a::AreaChart)
             fillto --> 0
             linecolor --> :black
             seriestype --> :path
             cumsum(a.args[1], dims = 2)[:,end:-1:1]
           end
    

    If you want to contribute a recipe to Plots you can open a pull request on Plots, or, eg. on StatsPlots - there's a good description of contributing here: https://docs.juliaplots.org/latest/contributing/

    It's a bit of reading, but very generally useful as an introduction to contributing to Julia packages.