daniel
daniel

Reputation: 11

Altair sorting Chart

I'm trying to plot the data of my DataFarme in a groupedChart and I want the columns to preserve the order I gave them before. The data looks as follows (its not all there but its in the same way organized) dataframe

When I plot it I get the following Graph: graph

So the months were sorted even though I specified not to sort in the chart. I used the following code:

chart2 = alt.Chart(melted).mark_bar().encode(
            column=alt.Column('variable',sort=None),
            x=alt.X('room',sort=None),
            y=alt.Y('value'),
            color='room',
            tooltip= ['room', 'value']
         
        )

Does anyone know how I could fix that?

Upvotes: 1

Views: 1252

Answers (1)

jakevdp
jakevdp

Reputation: 86443

You've already used sort=None, which is the correct way to make scales in a non-faceted chart reflect the input order.

The missing piece is that faceted charts share scales by default (See Scale and Guide Resolution), so each facet is being forced to share an order.

If you make the x scale resolution independent, then each facet should retain the input order:

chart2 = alt.Chart(melted).mark_bar().encode(
            column=alt.Column('variable',sort=None),
            x=alt.X('room',sort=None),
            y=alt.Y('value'),
            color='room',
            tooltip= ['room', 'value']
         
        ).resolve_scale(x='independent')

Upvotes: 1

Related Questions