Altair: Reverse/invert color scheme

Created on 3 Aug 2018  路  5Comments  路  Source: altair-viz/altair

I'd like to use the spectral color scheme for a heat map I'm working on, but I need to invert the color scheme to more intuitively match my data. (I'm plotting temperature data, and the spectral scheme defaults to red=low, blue/purple=high.) Is there a way in Altair to reverse/invert the color scheme?

I've tried adding reverse=True to alt.SchemeParams() based on a note under "Scheme Properties" in the Vega docs, but apparently this feature isn't supported by Vega-Lite.

The relevant part of my code looks basically like this:

chart = alt.Chart(data).mark_rect().encode(
    ... , 
    color=alt.Color('mean(temperature):Q',
                    scale=alt.Scale(domain=[lower_bound, upper_bound],
                                    clamp=True,
                                    scheme=alt.SchemeParams(name='spectral', reverse=True))))
question

Most helpful comment

Thank you for the help! Both approaches inverted the color scheme, but I ended up using the "sort": "descending" approach because the legend it produced works better for my data. (The "sort": "descending" approach produces a legend with the highest values at the top, whereas swapping the high and low domain values puts the high value at the bottom of the legend.)

heat map legends

All 5 comments

There is no reverse property in SchemeParams, so this will not work (the reverse argument you linked to is in the Vega docs, not the Vega-Lite docs, and Altair is built on Vega-Lite).

In your case, the easiest way to reverse the color scale is to reverse the domain: e.g. domain=[upper_bound, lower_bound].

It seems, though, that allowing SchemeParams to have a reverse argument that is propagated down to the Vega argument by the same name would be a useful enhancement to Vega-Lite.

Instead of reversing the scheme, you can sort the color encoding. Add "sort": "descending".

This feels like a bug though. We should take a look. I'll file a bug for now https://github.com/vega/vega-lite/issues/4116

Thank you for the help! Both approaches inverted the color scheme, but I ended up using the "sort": "descending" approach because the legend it produced works better for my data. (The "sort": "descending" approach produces a legend with the highest values at the top, whereas swapping the high and low domain values puts the high value at the bottom of the legend.)

heat map legends

Instead of reversing the scheme, you can sort the color encoding. Add "sort": "descending".

Thank you @domoritz!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pabloinsente picture pabloinsente  路  3Comments

bmcfee picture bmcfee  路  3Comments

morberg picture morberg  路  3Comments

floringogianu picture floringogianu  路  3Comments

DentonGentry picture DentonGentry  路  3Comments