question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse/invert color scheme

See original GitHub issue

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))))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
caleb-lindgrencommented, Feb 23, 2021

For anyone coming to this with the same problem, I couldn’t get the solution above to work (#2204). However, I was able to get the effect I wanted by passing reverse=True in an alt.Scale object to the alt.Color’s scale parameter:

import altair as alt
import numpy as np
import pandas as pd

# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z = x ** 2 - y ** 2

# Convert this grid to columnar data expected by Altair
source = pd.DataFrame({'x': x.ravel(),
                     'y': y.ravel(),
                     'z': z.ravel()})

alt.Chart(source).mark_rect().encode(
    x='x:O',
    y='y:O',
    color=alt.Color(
        "z:Q",
        scale=alt.Scale(
            scheme="redblue",
            reverse=True
        )
    )
)

image

4reactions
semiamauroticcommented, Aug 4, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invert a color online - PineTools
Inverted color · RGB. R G B · HSV. H S V · HSL. H S L.
Read more >
How to Invert the Colors on Your Computer
What Are Inverted Colors? ... Artists use a color wheel, a set of colors arranged in a circle, to help them select hues...
Read more >
Invert image (colors) online - Free tool
Invert image is a free online tool, helps to reverse the colors of image, where red color reversed to cyan, green reversed to...
Read more >
How To Invert Colors On Windows 10 Easily - MiniTool
Use Magnifier as Color Inverter · You can uncheck Invert colors or press Ctrl + Alt + I to disable inverted colors. ·...
Read more >
How to invert colors in Photoshop - Adobe
But if you invert a color image, the resulting photo will have a flipped color scheme. Photoshop will flip every color to its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found