繁体   English   中英

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

[英]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软件包创建于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编码来明确指定该order 文档中的一个示例在这里: https : //altair-viz.github.io/gallery/connected_scatterplot.html

就您而言,如果将order="index:Q"传递到编码列表,我相信结果将是您期望的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM