Merge pull request #1822 from JuliaPlots/revert-1821-revert-1819-mkb/plotrecipes

Revert "Revert "Transfer portfoliocomposition recipe from PlotRecipes""
This commit is contained in:
Daniel Schwabeneder 2018-10-30 16:26:58 +01:00 committed by GitHub
commit 2fef7a5b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -435,6 +435,24 @@ each line segment or marker in the plot.
end)]
),
PlotExample("Portfolio Composition maps",
"""
see: http://stackoverflow.com/a/37732384/5075246
""",
[:(begin
using Random
Random.seed!(111)
tickers = ["IBM", "Google", "Apple", "Intel"]
N = 10
D = length(tickers)
weights = rand(N,D)
weights ./= sum(weights, dims = 2)
returns = sort!((1:N) + D*randn(N))
portfoliocomposition(weights, returns, labels = permutedims(tickers))
end)]
),
]
# Some constants for PlotDocs and PlotReferenceImages
@ -446,6 +464,8 @@ _backend_skips = Dict(
:pgfplots => [2, 5, 6, 10, 16, 20, 22, 23, 25, 28, 30],
)
# ---------------------------------------------------------------------------------
# make and display one plot

View File

@ -1131,3 +1131,25 @@ end
title := string(grad.args[1])
z
end
# Moved in from PlotRecipes - see: http://stackoverflow.com/a/37732384/5075246
@userplot PortfolioComposition
# this shows the shifting composition of a basket of something over a variable
# - "returns" are the dependent variable
# - "weights" are a matrix where the ith column is the composition for returns[i]
# - since each polygon is its own series, you can assign labels easily
@recipe function f(pc::PortfolioComposition)
weights, returns = pc.args
n = length(returns)
weights = cumsum(weights, dims = 2)
seriestype := :shape
# create a filled polygon for each item
for c=1:size(weights,2)
sx = vcat(weights[:,c], c==1 ? zeros(n) : reverse(weights[:,c-1]))
sy = vcat(returns, reverse(returns))
@series Plots.isvertical(plotattributes) ? (sx, sy) : (sy, sx)
end
end