You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Julia中AlgebraOfGraphics包自定义颜色设置技术问询

Julia绘图自定义颜色设置(基于Colors包)

1. 按类别为箱线图着色(df1数据集)

要根据:nam列的类别给箱线图分配自定义颜色,可通过scale_color的palette参数将类别与Colors包的颜色一一对应:

using DataFrames
using Colors
using AlgebraOfGraphics
using CairoMakie

df1 = DataFrame( nam = ["a","a","a","b","b","b","c","c","c","c","c"], num = [1,2,3,1,2,3,4,3,2,1,1] )
# 定义类别对应的Colors包颜色
custom_colors = ["a" => firebrick, "b" => dodgerblue, "c" => forestgreen]
# 构建绘图对象,绑定颜色映射并应用自定义调色板
dataIn = data(df1) * mapping(:nam, :num, color=:nam) * visual(BoxPlot) * scale_color(; palette=custom_colors)
draw(dataIn)

2. 为连续变量设置自定义颜色渐变(df2数据集)

针对:t列的连续值,使用Colors包生成渐变范围,再通过scale_color绑定到颜色映射:

using DataFrames
using Colors
using AlgebraOfGraphics
using CairoMakie

df2 = DataFrame(t = [1,2,3,4,5,6], x = [1,1,1,1,1,1], y = [2,3,4,5,6,7])
# 用Colors包生成自定义渐变(从浅蓝到深蓝)
custom_gradient = range(colorant"lightblue", colorant"darkblue", length=10)
# 绑定颜色映射并应用渐变调色板
dataIn2 = data(df2) * mapping(:x, :y, color=:t) * scale_color(; palette=custom_gradient)
draw(dataIn2)

3. 直接传入Colors包的颜色到绘图

无需使用parse.(Colorant, ...),直接引用Colors包的颜色常量或colorant"颜色名"即可,分两种场景:

场景A:给所有元素设置单一颜色

using DataFrames
using Colors
using AlgebraOfGraphics
using CairoMakie

df2 = DataFrame(t = [1,2,3,4,5,6], x = [1,1,1,1,1,1], y = [2,3,4,5,6,7])
# 在visual中直接指定Colors包的颜色
dataIn2 = data(df2) * mapping(:x, :y) * visual(Scatter; color=goldenrod)
draw(dataIn2)

场景B:将颜色列表映射到数据列

using DataFrames
using Colors
using AlgebraOfGraphics
using CairoMakie

df2 = DataFrame(t = ["x","x","y","y","z","z"], x = [1,1,1,1,1,1], y = [2,3,4,5,6,7])
# 定义Colors包的颜色列表
color_list = [coral, teal, plum]
# 绑定数据列到颜色列表
dataIn2 = data(df2) * mapping(:x, :y, color=:t) * scale_color(; palette=color_list)
draw(dataIn2)

内容的提问来源于stack exchange,提问作者Mstislav Keldysh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:55:12