如何使用Plots.jl在Julia中制作堆积面积/折线图?

时间:2019-04-11 09:42:58

标签: plot julia stacked-area-chart

我想使用图在Julia中创建一个堆积面积图,例如类似to this

我知道/假设您可以直接在Julia中使用Gadfly或PyPlot后端,就可以这样做,但是我想知道是否有解决办法。如果没有,您如何为绘图食谱做出贡献?将是一个有用的补充。

2 个答案:

答案 0 :(得分:2)

有类似的食谱

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

由于某种原因,缩略图现在看起来已经坏了(但是代码可以正常工作)。

matlab示例中的精确图可以由

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

作为看起来像的食谱

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

如果您想为“地块”添加配方,则可以在“地块”上打开拉取请求,或者例如。在StatsPlots上-这里有一个很好的说明:https://docs.juliaplots.org/latest/contributing/

需要一些阅读,但是作为对Julia软件包的贡献的介绍,通常非常有用。

答案 1 :(得分:0)

您可以在朱莉娅(Julia)话语论坛上深入阅读this thread

使用Plots发布的一种解决方案是:

# 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)))

(由NiclasMattsson用户)

那里介绍的其他方法包括使用VegaLite.jl软件包。

相关问题