如何在不使用编码的情况下仅为一组绘制一个标记

时间:2019-05-18 01:42:19

标签: python altair

import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point(opacity=0.1).encode(
    x="Cylinders:O",
    y="Origin"
)

它在一个位置绘制许多点:

enter image description here

要在一个位置仅画一个点,我需要在count()上添加一个编码,

alt.Chart(cars).mark_point(opacity=0.3).encode(
    x="Cylinders:O",
    y="Origin",
    tooltip="count()"
)

enter image description here 或使用transform_aggregate(),但我需要设置groupby参数:

alt.Chart(cars).mark_point(opacity=0.4).encode(
    x="Cylinders:O",
    y="Origin",
).transform_aggregate(
    count="count()",
    groupby=["Cylinders", "Origin"]
)

我想知道是否有任何方法可以在没有transform_aggregate()count()的情况下执行此操作。

1 个答案:

答案 0 :(得分:3)

Altair将为数据的每一行显示一个点,除非您通过编码或转换显式传递了聚合。

如果您要应用对聚合没有影响的图表,那么最简单的方法是通过detail通道(这大致意味着“添加此编码但不添加此编码用它做任何事情”):

alt.Chart(cars).mark_point(opacity=0.4).encode(
    x="Cylinders:O",
    y="Origin:N",
    detail='count()'
)

enter image description here