为不同数据集着色图形代数 (Julia)

问题描述 投票:0回答:1

我正在使用图形代数按以下方式绘制多个数据集:

function filterPlot(filterPos)
    
    #filterPos - a vector of vectors 


     dataForPlot = data(DataFrame(xx = 1:length(filterPos[1]), yy = filterPos[1]))
        for thing in filterPos[2:end]
            plotFilter = DataFrame(xx = 1:length(thing), yy = thing)
            dataForPlot =dataForPlot + data(plotFilter)
        end

        
   
    
    dataForPlot *= mapping(:1=>(t->t) ,:2 => (t->t)); # data(toBePlotted)*mapping(:a =>(t->t) =>"Time (d)",:b => (t->t)=>"Weight")*linear();

    plaxis = (xlabel = xlab, ylabel = ylab)

    drawing = draw(dataForPlot;axis = plaxis,palettes=(; color=ColorSchemes.Set1_3.colors))
end

请注意,filterPos 中可以包含任意多个数据集。理想情况下,我希望它们中的每一个都以单独的颜色显示(我不需要手动设置)。 我知道 matplotlib 会自动给东西上色。 Makie 或 AlgebraofGraphics 中有类似的东西吗?

julia makie.jl
1个回答
0
投票

看起来您正在通过从传递给

filterPlot
的每个向量生成一堆单独的 DataFrame 对象来组合数据,然后通过有效地执行
AlgebraOfGraphics.data(df1) + AlgebraOfGraphics.data(df2) + ...
对它们求和。这是可行的,但如果您只是希望每个 DataFrame 在绘图中具有不同的色调,同时对所有这些 DataFrame 应用相同的
mapping
visual
,请考虑将 DataFrame first 与标识的虚拟变量结合起来原点向量。

为此,首先将向量组成的向量组合成一个长 DataFrame:

using DataFrames, CairoMakie, AlgebraOfGraphics

# fake data: 5 vectors of random size containing lines of data with noise
rand_line(slope, n) = slope .* (1:n) .+ randn(n)
filterPos = [rand_line(slope, rand(10:20)) for slope in 1:5]

# a function to convert a Vector to a DataFrame in the format you want
vec_to_df((id, values)) = DataFrame(label = id, xx = 1:length(values), yy = values)

# combine all the vectors by mapping our vec_to_df function to each and reducing with vcat
df = mapreduce(vec_to_df, vcat, enumerate(filterPos))

此时,您可以按照 AlgebraOfGraphics 文档中的

linear
函数示例

# plot, using the :label column as non-numeric to separate datasets
draw(data(df) * mapping(:xx, :yy, color = :label => nonnumeric) * (linear() + visual(Scatter)))

结果:

A plot of points lying along five separate lines of different slopes, each with a different color

© www.soinside.com 2019 - 2024. All rights reserved.