首页 文章

Julia Plots.jl:改变系列食谱中的标记颜色

提问于
浏览
2

我正在尝试整理一个 Plots.jl 用户配方,这将创建排列在网格中的几个散点图和直方图 . 散点图应占据网格的下三角形 . 直方图在对角线上 . 每个散点图应该有几个系列,以不同的颜色显示 . 子图中的颜色应该是一致的 .

我的问题是当我允许用户指定自定义颜色时,散点子图中的系列颜色会搞砸 .

这是(删节)食谱代码 . data 是一个大小为 r 的向量 . data 的每个元素都是大小为 n_r x d 的矩阵 . n_r 可能会有所不同, d 保持不变 . 应该有 d x (d-1) / 2 散点图,每个图上有 r 系列,每个系列都有 n_r 点 .

@recipe function my_func(data::my_type; custom_colors=nothing)
    # get d, r, ...
    for i in i:d
        for j in 1:d
            @series begin
                subplot := (i - 1) * d + j
                if i == j
                    seriestype := :histogram
                    plot_data = # ... prepare data for histograms
                elseif j < i
                    # scatter subplot recipe
                    seriestype := :scatter
                    if custom_colors !== nothing
                        color := reshape(custom_colors, (1, r))
                    end
                    x = Vector()
                    y = Vector()
                    for r in runs
                        ser = data[r]
                        append!(x, [ser[:, j]])
                        append!(y, [ser[:, i]])
                    end # for r
                    plot_data = (x, y)
                else
                    # leave empty
                    plot_data = [0]
                end #  if/else
                plot_data  # return from the macro function
            end # @series
        end # for j
    end # for i
end # @recipe

我的问题是,无论何时我提供自定义颜色,我都会在子图中出现不一致:

plot(my_data, custom_colors=["blue", "green", "black"])

plot with custom colors

注意子图(2,1)中间是否有黑点,而所有其他散点子图在外面都是黑色的

如果我没有自定义颜色的情节:

plot(my_data)

我在所有散点子图中都得到了一致的颜色:

enter image description here

有关为什么颜色在第一个情节上混淆的任何线索?

1 回答

  • 0

    好的,这里的诀窍是每个散点图上的每个系列都应该用自己的宏绘制 . 换句话说, @series ... end 应该进入 for r in runs ... end . 并为代码的每个其他分支单独的 @series 宏 .

相关问题