Eamonn
Eamonn

Reputation: 617

Set a diverging color scheme to center on zero in Altair

How can I go about getting the colour scale to center on zero for a diverging colour scale.

Upvotes: 3

Views: 2334

Answers (2)

rturquier
rturquier

Reputation: 387

We can now use domainMid=0.

Adapting the example from @jakevdp:

df = pd.DataFrame(np.random.randn(100, 3), columns=['x', 'y', 'z'])
alt.Chart(df).mark_point().encode(
    x='x',
    y='y',
    color=alt.Color('z', scale=alt.Scale(scheme='blueorange', domainMid=0))
)

enter image description here

Upvotes: 3

jakevdp
jakevdp

Reputation: 86330

One way to do this is to explicitly set the domain to be symmetric:

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

df = pd.DataFrame(np.random.randn(100, 3), columns=['x', 'y', 'z'])
alt.Chart(df).mark_point().encode(
  x='x',
  y='y',
  color=alt.Color('z', scale=alt.Scale(scheme='blueorange', domain=[-3, 3]))
)

enter image description here

Upvotes: 3

Related Questions