我发现mark_line
的point
属性和Altair/Vega Lite colored颜色 图例的出现之间存在一些不直观的相互作用.我在try 创建一条包含非常大且大部分透明点的线条时遇到了这种情况,以增加触发线条tooltip
的面积,但无法保留可见的type=gradient
图例.
以下代码是针对该问题的MRE,显示了6种情况:对point属性使用[False
、True
和自定义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):
每个图的 colored颜色 数据都是相同的,但 colored颜色 图例到处都是.在我的真实 case 中,gradient
是首选(数据是定量和连续的).
-
mark_line
上没有point
,图例是正确的. - 添加
point=True
将图例转换为symbol
类型——我不确定为什么会出现这种情况,因为定量数据的默认图例类型是gradient
(如第一行所示),这是相同的数据——但可以通过自定义编码强制返回到gradient
. - 但是,try 通过
OverlayMarkDef
创建自定义点会使强制gradient
colored颜色 条不可见,这与OverlayMarkDef
中的opacity
相匹配.但这不仅仅是因为传说总是继承point
的属性,因为symbol
的传说并不试图反映opacity
.
我想为定制的OverlayMarkDef
款提供普通渐变色条,但我也很想为这里的情况建立一些直觉.