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

如何为Altair直方图添加7种不同的颜色分箱?

实现Altair直方图多分箱颜色配置

原来的alt.condition确实只能处理单一阈值的二分情况,要实现最多7种颜色分箱,有两种实用方案:

方案1:嵌套alt.condition

通过多层嵌套alt.condition实现多阈值判断,每个层级对应一个颜色区间。比如按Creditworthiness_bin_end的阈值划分7个区间,每个区间匹配对应颜色:

alt.Chart(X_train).transform_bin(
    'Creditworthiness_bin', 'Creditworthiness', bin=alt.Bin(step=10)
).transform_joinaggregate(
    count='count()', groupby=['Creditworthiness_bin']  
).mark_bar(orient='vertical').encode(
    alt.X('Creditworthiness_bin:Q', bin='binned'),
    alt.X2('Creditworthiness_bin_end'),
    alt.Y('count:Q'),
    color=alt.condition(
        alt.datum.Creditworthiness_bin_end <= 30,
        alt.value("red"),
        alt.condition(
            alt.datum.Creditworthiness_bin_end <= 40,
            alt.value("yellow"),
            alt.condition(
                alt.datum.Creditworthiness_bin_end <= 50,
                alt.value("green"),
                alt.condition(
                    alt.datum.Creditworthiness_bin_end <= 60,
                    alt.value("steelblue"),
                    alt.condition(
                        alt.datum.Creditworthiness_bin_end <= 70,
                        alt.value("orange"),
                        alt.condition(
                            alt.datum.Creditworthiness_bin_end <= 80,
                            alt.value("purple"),
                            alt.value("gray")  # 兜底颜色
                        )
                    )
                )
            )
        )
    )
)

这种方法适合阈值较少的场景,但嵌套层级过多会导致代码冗余。

方案2:先计算分组字段再映射颜色(推荐)

通过transform_calculate生成分组标签字段,再将该字段与颜色映射绑定,这种方式更清晰易维护,适合多分组场景:

alt.Chart(X_train).transform_bin(
    'Creditworthiness_bin', 'Creditworthiness', bin=alt.Bin(step=10)
).transform_joinaggregate(
    count='count()', groupby=['Creditworthiness_bin']
).transform_calculate(
    # 根据bin_end的值生成分组标签
    color_group=alt.condition(alt.datum.Creditworthiness_bin_end <=30, "'组1'",
        alt.condition(alt.datum.Creditworthiness_bin_end <=40, "'组2'",
            alt.condition(alt.datum.Creditworthiness_bin_end <=50, "'组3'",
                alt.condition(alt.datum.Creditworthiness_bin_end <=60, "'组4'",
                    alt.condition(alt.datum.Creditworthiness_bin_end <=70, "'组5'",
                        alt.condition(alt.datum.Creditworthiness_bin_end <=80, "'组6'", "'组7'"))))))
).mark_bar(orient='vertical').encode(
    alt.X('Creditworthiness_bin:Q', bin='binned'),
    alt.X2('Creditworthiness_bin_end'),
    alt.Y('count:Q'),
    color=alt.Color('color_group:N',
        scale=alt.Scale(
            domain=['组1', '组2', '组3', '组4', '组5', '组6', '组7'],
            range=['red', 'yellow', 'green', 'steelblue', 'orange', 'purple', 'gray']
        ),
        legend=alt.Legend(title='信用分区间')
    )
)

代码说明:

  1. transform_calculate通过嵌套条件生成color_group字段,每个区间对应一个分组标签;
  2. color编码时,将color_group字段与自定义颜色列表绑定,通过scale.domain和scale.range一一对应分组和颜色;
  3. 可通过legend参数自定义图例名称,提升图表可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 23:01:13