Altair 排序在多面图表上无法正常工作

问题描述 投票:0回答:1

我有一些多面条形图,显示不同绘画中不同颜色的数量。

base = alt.Chart(df).mark_bar().encode(
        x=alt.X(
            'color',
            title='',
        ),
        y=alt.Y(
            'amount',
            title='',
            ),
        color=alt.Color('color',scale= alt.Scale(domain=rp,range=rph))
    ).properties(
        width=80,
        height=80
    ).facet(
        facet='TITLE',
        columns=5
    )

这是第一行: enter image description here

我想对每个图表按 x 轴降序排序,所以我添加:

x=alt.X(
  'color',
  title='',
  sort='-y'
)

结果如下: enter image description here

第二张和第五张图几乎排序正确,但不完全正确,第一张和第三张图也是如此,但由于某种原因它们正在升序。什么会导致这种奇怪的行为?和面相有关系吗?

python altair
1个回答
0
投票

添加 .resolve_scale(x='independent') 是否会产生所需的输出?

import altair as alt

base = (
    alt.Chart(df)
    .mark_bar()
    .encode(
        x=alt.X(
            "color",
            title="",
            sort="-y",
        ),
        y=alt.Y(
            "amount",
            title="",
        ),
        color=alt.Color("color", scale=alt.Scale(domain=rp, range=rph)),
    )
    .properties(width=80, height=80)
    .facet(facet="TITLE", columns=5)
    .resolve_scale(x="independent")
)
© www.soinside.com 2019 - 2024. All rights reserved.