altair icon indicating copy to clipboard operation
altair copied to clipboard

Faceting bug for categorical columns

Open wirhabenzeit opened this issue 5 months ago • 29 comments

What happened?

Faceting by pl.Categorical columns results in wrong facets

alt.Chart(
    pl.from_pandas(vega_datasets.data.cars()).with_columns(
        pl.col("Origin").cast(pl.Categorical),
        pl.col("Cylinders").cast(pl.String).cast(pl.Categorical),
    )
).mark_point().properties(width=150, height=150).encode(
    x="Horsepower",
    y="Miles_per_Gallon",
    shape="Cylinders",
    color=alt.Color("Origin").scale(scheme="category10"),
).facet(row="Origin", column="Cylinders")

facets-not-ok

I am not exactly sure what is going wrong, but suddenly all American cars are in the Europe facet, some European cars are in the Japan facet, the Japanese cars are in the correct facet, the 4-Cylinder cars are in the 5 and 6-Cylinder facets, etc. (There is probably some obvious pattern here which I am missing)

I checked the Vega-Lite output and I think the issue is the sort parameter of the resulting spec file.

What would you like to happen instead?

The same code with pl.String columns works as expected:

alt.Chart(
    pl.from_pandas(vega_datasets.data.cars()).with_columns(
        pl.col("Origin"), 
        pl.col("Cylinders").cast(pl.String)
    )
).mark_point().properties(width=150, height=150).encode(
    x="Horsepower",
    y="Miles_per_Gallon",
    shape="Cylinders",
    color=alt.Color("Origin").scale(scheme="category10"),
).facet(row="Origin", column="Cylinders")

facets-ok

Which version of Altair are you using?

5.4.1

wirhabenzeit avatar Sep 11 '24 16:09 wirhabenzeit