当 x 轴包含重复项时,mark_line in altair/vega-lite 对数据重新排序

mark_line in altair/vega-lite reorders the data when x axis contains duplicates

我注意到,当 x 轴上有重复且 y 轴上有不同的值时,不会考虑提供数据的顺序。最大值链接到之前的点,最小值链接到下一个点。例如,在创建 CDF(累积分布函数)时,这不是我所期望的。

我尝试为索引提供 EncodingSortField,但这不起作用。我可以通过删除数据中具有最小值的行来绘制我想要的图表,但是我需要手动添加点。

这是设计使然吗?还是我遗漏了什么?

下面是一个可重现的例子。

import pandas as pd
import altair as alt

df = pd.DataFrame({'x':[-1, 0, 0, 1, 2],
                   'y':[-1, 0, 1, 2, 3],
                   'index':[0, 1, 2, 3, 4]})

step = alt.Chart(df).mark_line(interpolate="step", point=True).encode(
    x='x:Q', 
    y='y:Q',
).properties(width=150, 
             height=150, 
             title="interpolate='step'")

step_after = step.mark_line(
    interpolate='step-after', 
    point=True
).properties(title="interpolate=step-after")

step_before = step.mark_line(
    interpolate='step-before', 
    point=True
).properties(title="interpolate=step-before")

sort = step.encode(
    y=alt.Y('y:Q', 
            sort=alt.EncodingSortField(field='index', 
                                       op='sum'))
).properties(title='sort by index')

expected = (step_before.properties(data=df[df.index != 1], 
                                   title='expected') + 
            alt.Chart(pd.DataFrame([{'x':0, 
                                     'y':0}])
                     ).mark_circle().encode(
                x='x:Q', y='y:Q')
           )

(step | step_before | step_after) & (sort | expected)

reprexpy package

于 2018-11-15 创建
import reprexpy
print(reprexpy.SessionInfo())
#> Session info --------------------------------------------------------------------
#> Platform: Darwin-18.2.0-x86_64-i386-64bit (64-bit)
#> Python: 3.6
#> Date: 2018-11-15
#> Packages ------------------------------------------------------------------------
#> altair==2.2.2
#> pandas==0.23.4
#> reprexpy==0.2.1

谢谢。

传递到 Altair 的数据行的顺序不会保留在图表输出中,这是设计使然。

如果您希望数据条目以特定顺序绘制,您可以使用 order 编码来明确指定;文档中的示例如下:https://altair-viz.github.io/gallery/connected_scatterplot.html

在您的情况下,如果您将 order="index:Q" 传递给您的编码列表,我相信结果会如您所愿。