altair: Faceting bug for categorical columns
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")
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")
Which version of Altair are you using?
5.4.1
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 29 (23 by maintainers)
Yeah, The referenced VL issues are relevant here.
But just to be complete, what is happening. Altair tries to sort the fields in your column ascending when defined as type
stron an encoding channel.So when having this data:
And visualising it with the
monthon thex-axis channel and thevalueson thecolorchannel using arect-markIt can be seen that the
x-axis is ordered byfeb,jan,mar, since thefcomes forjin the alphabet.So by casting the
monthcolumn in the dataframe as being acategoricalin order of appearance (default of polars). We get the following:The
x-axis is now ordered byjan,feb,mar, like the order as is defined in the dataframe.By comparing the Vega-Lite specification of both charts we notice that the
categoricalcolumn is serialised differently.Top chart, column is of type
strand it becomes:With categorical column defined, it becomes:
The
sortorder is serlialized from the categorical definition of themonthcolumn in the DataFrame. All good so far!But upon adding encoding channels such as
rowandcolumnthis logic for sorting categorical columns in the DataFrame is breaking the rendering when there are combinations that contains no data.The following goes well, but the order of the
columnmay be seen as not right.So upon defining the column
monthas categorical, the order of the months in thecolumnencoding is correctly sorted, but the data within the plots are incorrect.Leading indeed to data being drawn within the wrong subplot! So, indeed, be very careful here!
One can use the following workaround when having a polars DataFrame as in OP:
This workaround basically makes sure that all combinations that are possible to make with the row/column channel encoding, are actually existing in the DataFrame, albeit filled with a
nullvalue.Pff, complicated. Altair assumes that the returned categories are in sorted order when it is defined as ordered, but this is an assumption that does not always hold.
get_categories()returns the custom order as is defined. Result is that the custom order list fromget_categories()is used tosortthey-encoding channel in the following chart.get_categories()returns physical categorical sorted in physical order. Result is that the physical ordered list fromget_categories()is used tosortthey-encoding channel in the following chart.get_categories()does not return lexical categorical sorted in lexical order. Result is that the list fromget_categories()is not used for they-encoding channel. Since there is nosortdefined it applies ascending sorting within Vega, making it look like that the lexical categorical has impact.To support lexical categorical, it should
get_categories()should be reflecting the lexical order.Current implemention of
nw.is_ordered_categoricalonly allows order to be defined based on numeric values and not on alphabet (lexical).Apparently the situation is different for pandas ordered categorical. Since it does not always return the sorted physical ordered categorical.
Btw. When trying OP as you did in https://github.com/vega/altair/issues/3588#issuecomment-2347221524. I get this:
With
('5.5.0dev', '1.6.2')foralt.__version__, nw.__version__. Reflecting the behaviour you have when using Altair version5.3.0…Thanks for the ping!
I think they should be the same? As in,
pl.Enumshould be recognised asnw.Enum:Regarding
physicalvslexical, I don’t think thatget_categoriesreflects the order - but maybe it should? The difference can be seen if you compare the categories, e.g. in a sort:but they both return the same output for
.cat.get_categories(). Do you need the output of.cat.get_categoriesto reflect the category ordering?nw.is_ordered_categoricaljust does what Polars does in its dataframe interchange protocol definition:https://github.com/pola-rs/polars/blob/501988ea1c2a114e4c28619727157354211af93a/py-polars/polars/interchange/column.py#L60-L78
the interchange protocol definition is a bit vague here, it just says “whether the ordering of dictionary indices is semantically meaningful”
@mattijn no worries, yeah you’ve understood that correctly
Thanks for adding more info on the table! But I’m not sure if I can read an answer in this already. Or can I understand from here that a categorical with
dtype.ordering == "lexical"is intentionaly not ordered? And therefor casting topl.Categorical('lexcial')is correctly not adding asortargument to the Vega-Lite specification?