Thomas Becker
Thomas Becker

Reputation: 43

Is there a way to reverse the color scheme of an altair choropleth map with binned data?

So I am trying to create a choropleth map at the county level using binned data in Altair. I got the bin function to work, but I can't reverse the direction of the color scheme. If I use the following argument I can change the direction of the color scheme with non-binned data:

sort="descending"

With the binned data it doesn't get an error but the sort argument doesn't do anything, here is the full code I have been messing with using an vega-lite gallery example map:

import altair as alt
from vega_datasets import data

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

alt.Chart(counties).mark_geoshape().encode(
    color=alt.Color('rate:Q', bin=alt.Bin(maxbins=7), sort="descending", scale=alt.Scale(scheme='yelloworangered'))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).project(
    type='albersUsa'
).properties(
    width=500,
    height=300
)

Is there a way to reverse the color scheme with binned data? What I want in this example is to make the higher unemployment rate counties yellow and the lower ones red.

Upvotes: 2

Views: 675

Answers (1)

joelostblom
joelostblom

Reputation: 48919

You can reverse any scale in Altair by setting reverse=True (no need to sort). In your case:

scale=alt.Scale(scheme='yelloworangered', reverse=True)

enter image description here

Upvotes: 5

Related Questions