我发现mark_linepoint属性和Altair/Vega Lite colored颜色 图例的出现之间存在一些不直观的相互作用.我在try 创建一条包含非常大且大部分透明点的线条时遇到了这种情况,以增加触发线条tooltip的面积,但无法保留可见的type=gradient图例.

以下代码是针对该问题的MRE,显示了6种情况:对point属性使用[FalseTrue和自定义OverlayMarkDef],以及使用普通和自定义color编码.

import pandas as pd
import altair as alt

# create data
df = pd.DataFrame()
df['x_data'] = [0, 1, 2] * 3
df['y2'] = [0] * 3 + [1] * 3 + [2] * 3

# initialize
base = alt.Chart(df)
markdef = alt.OverlayMarkDef(size=1000, opacity=.001)
color_encode = alt.Color(shorthand='y2', legend=alt.Legend(title='custom legend', type='gradient'))
marks = [False, True, markdef]
encodes = ['y2', color_encode]

plots = []
for i, m in enumerate(marks):
    for j, c in enumerate(encodes):
        plot = base.mark_line(point=m).\
            encode(x='x_data', y='y2', color=c, tooltip=['x_data','y2']).\
            properties(title=', '.join([['False', 'True', 'markdef'][i], ['plain encoding', 'custom encoding'][j]]))
        plots.append(plot)
combined = alt.vconcat(
    alt.hconcat(*plots[:2]).resolve_scale(color='independent'),
    alt.hconcat(*plots[2:4]).resolve_scale(color='independent'),
    alt.hconcat(*plots[4:]).resolve_scale(color='independent')
).resolve_scale(color='independent')

The resulting plot (the interactive tooltips work as expected): enter image description here

每个图的 colored颜色 数据都是相同的,但 colored颜色 图例到处都是.在我的真实 case 中,gradient是首选(数据是定量和连续的).

  • mark_line上没有point,图例是正确的.
  • 添加point=True将图例转换为symbol类型——我不确定为什么会出现这种情况,因为定量数据的默认图例类型是gradient(如第一行所示),这是相同的数据——但可以通过自定义编码强制返回到gradient.
  • 但是,try 通过OverlayMarkDef创建自定义点会使强制gradient colored颜色 条不可见,这与OverlayMarkDef中的opacity相匹配.但这不仅仅是因为传说总是继承point的属性,因为symbol的传说并不试图反映opacity.

我想为定制的OverlayMarkDef款提供普通渐变色条,但我也很想为这里的情况建立一些直觉.

推荐答案

自Altair 4.2.0以来,右下角绘图的透明度问题已得到修复,因此现在所有包含point的场景都将图例改为"序号",而不是"数量".

enter image description here

我认为图例被转换为符号而不是渐变的原因是,您正在添加填充点,而fill通道未设置为定量字段,因此默认为ordinalnominal,带有排序:

plot = base.mark_line().encode(
    x='x_data',
    y='y2',
    color='y2',
)
plot + plot.mark_circle(opacity=1)

enter image description here

mark_point给出了一个渐变图例,因为它没有填充,如果我们明确地将填充设置为mark_circle,我们也会得到一个渐变图例(一个用于fill,一个用于color).

plot = base.mark_line().encode(
    x='x_data',
    y='y2',
    color='y2',
    fill='y2'
)
plot + plot.mark_circle(opacity=1)

enter image description here

我同意你的看法,这有点出乎意料,如果将point=True的编码类型设置为与用于行的编码类型相同,则会更方便.您可能会建议这是VegaLite中的一个增强,同时报告无法通过type='gradient'覆盖图例类型的明显错误.

Python相关问答推荐

使用LineConnection动画1D数据

使用新的类型语法正确注释ParamSecdecorator (3.12)

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何记录脚本输出

django禁止直接分配到多对多集合的前端.使用user.set()

Python虚拟环境的轻量级使用

在Python中动态计算范围

海上重叠直方图

部分视图的DataFrame

如何在turtle中不使用write()来绘制填充字母(例如OEG)

索引到 torch 张量,沿轴具有可变长度索引

如何在FastAPI中为我上传的json文件提供索引ID?

如何使regex代码只适用于空的目标单元格

未调用自定义JSON编码器

剪切间隔以添加特定日期

人口全部乱序 - Python—Matplotlib—映射

如何用FFT确定频变幅值

pytest、xdist和共享生成的文件依赖项

为什么按下按钮后屏幕的 colored颜色 保持不变?

如何将验证器应用于PYDANC2中的EACHY_ITEM?