Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: stacked area plot #1423

Closed
MFairley opened this issue Mar 5, 2018 · 11 comments
Closed

Feature request: stacked area plot #1423

MFairley opened this issue Mar 5, 2018 · 11 comments

Comments

@MFairley
Copy link

MFairley commented Mar 5, 2018

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.

@mkborregaard
Copy link
Member

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

@mkborregaard
Copy link
Member

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

@MFairley
Copy link
Author

MFairley commented Mar 5, 2018

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.

@pkofod
Copy link
Contributor

pkofod commented Mar 5, 2018

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.

@niclasmattsson
Copy link

niclasmattsson commented Apr 24, 2018

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.

@mkborregaard
Copy link
Member

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

@mariok90
Copy link

mariok90 commented Sep 14, 2018

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

@mbauman
Copy link

mbauman commented May 21, 2019

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)])).

@daschw
Copy link
Member

daschw commented May 23, 2019

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)

@junglegobs
Copy link

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?

@mkborregaard
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants