Learn how to order a categorical variable in Altair.

Ordering involves specifying the order of values in a categorical variable. In Altair, we can determine the order of an encoding channel by passing the sort parameter to it.

How to use ordering in Altair

To order a specific encoding channel, set the sort parameter according to a particular criterion.

Consider the following example, which uses the sample dataset and creates a sorted horizontal bar chart in Altair. We use the sort parameter to specify the category’s order along the y-axis. Specifically, use alt.SortField('Value', order='descending') to sort the categories based on the Value variable in descending order.

Get hands-on with 1200+ tech skills courses.