Plots.jl: Feature request: stacked area plot

Created on 5 Mar 2018  Â·  11Comments  Â·  Source: JuliaPlots/Plots.jl

Would it be possible to create a convenient way to create stacked area plots? E.g. http://johnmyleswhite.github.io/Vega.jl/areaplot.html

A normalize option would be very useful to create a 100% stacked area plot.

Most helpful comment

In case a simple stackedarea recipe is still needed. This works fine for me:

@userplot Areaplot
@recipe function f(a::Areaplot)
    data = cumsum(a.args[1], 2)
    seriestype := :line
    fillrange := 0
    @series begin
        data[:,1]
    end
    for i in 2:size(data, 2)
    @series begin
            fillrange := data[:,i-1]
            data[:,i]
        end
    end
end

All 11 comments

I'm pretty sure we have that functionality? Let me check.

Could you look at this and say if that comes close or you'd need something else?
https://github.com/JuliaPlots/PlotRecipes.jl#portfolio-composition-maps

Yes, that recipe is working for me. I modified it slightly for my use by switching sx and sy. I also see that PyPlot as a stackplot function but it is not exported by Plots.

I also see that PyPlot as a stackplot function but it is not exported by Plots.

Plots doesn't really use PyPlot to draw what you see, recipes define the different plots you can chose from.

That example results in an error on Julia 0.6.2 (ERROR: MethodError: no method matching transpose(::String)). Here's a fix:

using PlotRecipes
tickers = ["IBM" "Google" "Apple" "Intel"]
N = 10
D = length(tickers)
weights = rand(N,D)
weights ./= sum(weights, 2)
returns = sort!((1:N) + D*randn(N))

portfoliocomposition(weights, returns, labels = tickers)

A suggestion. Stacked area plots have lots of uses outside the world of finance. How about just calling this recipe stackedarea() instead of portfoliocomposition()? It would make discovery a lot easier for most users. EDIT: Also, flipping the x & y axes and the order of the weights & returns arguments would probably be better defaults. See MFairley's comment above.

All of those are good ideas :+1:
Do you feel like trying your hand at updating the recipe and sending a PR with the name and defaults you're suggesting?

In case a simple stackedarea recipe is still needed. This works fine for me:

@userplot Areaplot
@recipe function f(a::Areaplot)
    data = cumsum(a.args[1], 2)
    seriestype := :line
    fillrange := 0
    @series begin
        data[:,1]
    end
    for i in 2:size(data, 2)
    @series begin
            fillrange := data[:,i-1]
            data[:,i]
        end
    end
end

Thanks for posting that @mariok90 — I needed this and your recipe was greatly appreciated. I updated it for 1.0 and added the ability to pass the x-axis as the first argument to AreaPlot:

@userplot AreaPlot
@recipe function f(a::AreaPlot)
    data = cumsum(a.args[end], dims=2)
    x = length(a.args) == 1 ? (1:size(data, 1)) : a.args[1]
    seriestype := :line
    fillrange := 0
    @series begin
        x, data[:,1]
    end
    for i in 2:size(data, 2)
        @series begin
            fillrange := data[:,i-1]
            x, data[:,i]
        end
    end
end

Use like plot(AreaPlot([-5:5, rand(11, 3)])).

Use like plot(AreaPlot([-5:5, rand(11, 3)]))

or

areaplot(-5:5, rand(11, 3))

(or

areaplot!(-5:5, rand(11, 3))

to add to an existing plot)

I tried using the portfoliocomposition recipe recently and I got the following error:

julia> using Plots

julia> x = [1 2 3]
1×3 Array{Int64,2}:
 1  2  3

julia> y = rand(3,3)
3×3 Array{Float64,2}:
 0.403945   0.734758  0.702108
 0.480646   0.192144  0.323393
 0.0790197  0.450067  0.812049

julia> portfoliocomposition(x,y)
ERROR: UndefKeywordError: keyword argument dims not assigned

Which seems very weird...

I would also like to second the suggestion that portfoliocompositon be changed to something more general.

Use like plot(AreaPlot([-5:5, rand(11, 3)])).

Is this recipe available as part of Plots? Do I need to add a package to use it or currently do I just need to define the above recipe myself?

We should just add the AreaPlot as a recipe to Plots.
The error is a pre-post julia-1.0 thing I think.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cortner picture cortner  Â·  4Comments

crstnbr picture crstnbr  Â·  3Comments

ereday picture ereday  Â·  3Comments

nebuta picture nebuta  Â·  3Comments

apalugniok picture apalugniok  Â·  3Comments