如何突出显示 Altair 热图中的列?

How to highlight columns in an Altair heatmap?

我想将 reader 的注意力引导到热图中的某些列(或行和列),同时仍保留完整的上下文。

我可以使用 alt.condition 来改变颜色和不透明度。两者都在一定程度上起作用。但是不透明度的变化与值的变化以类似的方式可视化。使用不同的颜色会改变人们对价值观的看法。我想做的是在我要突出显示的连续列周围放置黄色或红色边框。

这就是我现在拥有的。还有其他想法吗?

    import altair as alt
    alt.data_transformers.disable_max_rows()
    
    def create_att_chart(df, keys_to_highlight=[], width=150, height=150, title=None, labels_x=True, labels_y=True):
        
        properties = {}
        if title:  
            properties['title'] = title  
         
        if width:  properties['width']  = width
        if height: properties['height'] = height
            
        chart = alt.Chart(df).mark_rect().encode(
            x=alt.X('k:N', sort=None, axis=alt.Axis(labels=labels_x, title=None, ticks=False), title=None),
            y=alt.Y('q:N', sort=None, axis=alt.Axis(labels=labels_y, title=None, ticks=False), title=None),
            opacity=alt.Opacity('a:Q', legend=None),
            column=alt.Column('h:N', title=None, header=alt.Header(labels=False), spacing=0.), 
            row=   alt.Row(   'l:N', title=None, header=alt.Header(labels=False), spacing=5.))
            
        if keys_to_highlight:
            chart = chart.encode(
                color=alt.condition(
                    alt.Predicate(alt.FieldOneOfPredicate(field='k', oneOf=keys_to_highlight)),
                    alt.value('orange'),
                    alt.value('blue')))
        else:
            chart = chart.encode(color=alt.value('blue'))
        
        return chart.properties(**properties)

    [..]

    ((create_att_chart(df_pt, ['sage', '##maker'], title='Pre-Trained') | create_att_chart(df_ft, ['sage', '##maker'], title='Fine-Tuned', labels_y=False)).properties(padding=0))

您可以尝试使用 stroke 编码而不是 color 的条件,但我认为这会给您每个框周围的笔触,这可能不是您想要的。相反,您可以将 mark_rulemark_rect 与文档中的此示例一起使用:

import altair as alt
import numpy as np
import pandas as pd

# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z = x ** 2 + y ** 2
# Convert this grid to columnar data expected by Altair
source = pd.DataFrame({'x': x.ravel(), 'y': y.ravel(), 'z': z.ravel()})

heatmap = alt.Chart(source).mark_rect().encode(
    x='x:O',
    y='y:O',
    color=alt.Color('z:Q', scale=alt.Scale(scheme='blues')))

现在添加规则:

rule1 = alt.Chart(df).mark_rule(stroke='orange', strokeWidth=2).encode(x=alt.value(20))
rule2 = alt.Chart(df).mark_rule(stroke='orange', strokeWidth=2).encode(x=alt.value(60))
heatmap + rule1 + rule2

顶级规则可能更 appealing/elegant 如果需要,您可以在其上方添加文本 mark_text:

rule1 = alt.Chart(df).mark_rule(stroke='orange', strokeWidth=3).encode(
    y=alt.value(-5),
    x=alt.value(20),
    x2=alt.value(60))
heatmap + rule1

mark_rect 有效,但在正方形中间添加线条,因为比例是有序的,定量的 mark_rect 弄乱了轴:

df = pd.DataFrame({'x': [0], 'x2': [3]})
box = alt.Chart(df).mark_rect(color='', stroke='orange', strokeWidth=2).encode(
    x='x:O',
    x2=alt.X2('x2:O', title='x'))
heatmap + box

如果您尝试在两者之间添加线条,将创建新的序轴标记。您可以滥用它并通过分隔使线条变白以突出显示,但轴上的刻度仍然存在,因此您必须使用 lablExpr 或类似的方法将其删除。

df = pd.DataFrame({'x': [0.5], 'x2': [3.5]})
box = alt.Chart(df).mark_rect(color='', stroke='white').encode(
    x='x:O',
    x2=alt.X2('x2:O', title='x'))
(heatmap + box).configure_view(stroke=None)