如何将该密度图的中间 50% 着色为不同的蓝色(或不同的 alpha 透明度)?
using CairoMakie
f = Figure()
Axis(f[1, 1])
density!(randn(200))
f
最佳答案
这个答案是 @flurble 答案的发展,它完成了寻找正确选项和参数的所有困难。为了使其更可用,我添加了一个辅助函数,并将使用分类颜色图切换为连续颜色图(更易于操作)。代码如下所示:
using CairoMakie, Colors, ColorSchemes, Random, StatsBase
# generate 'square' color gradient
in_out_colorscheme(outcolor, incolor, eps=0.00000001) =
cgrad([outcolor, incolor, incolor, outcolor], [0.0,eps,1.0-eps,1.0])
out_blue = HSLA(200,0.9,0.8,0.8)
in_blue = HSLA(200,0.9,0.4,0.8)
blue_in_blue = in_out_colorscheme(out_blue, in_blue);
# generate some data
data = begin
Random.seed!(14)
randn(200)
end;
# the region to highlight will be a 50% quantile interval
your_x_min, your_x_max = quantile(data, (0.25,0.75))
begin
f = Figure()
ax = Axis(f[1, 1])
density!(data,
# strokecolor = out_blue, strokewidth = 2, strokearound = true,
color=:x,
colormap=blue_in_blue,
colorrange=(your_x_min,your_x_max)
)
f
end
结果是:
PS 奇怪的 begin
-end
block 源自编写此代码的 Pluto 笔记本单元。
关于plot - 在 Makie.jl 中,如何对密度图的范围进行着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73629698/