Reputation: 67
I am using Altair for Python and my current heatmap code uses a redyellowblue color scheme (A) that uses yellow as the middle color. I am trying to edit this color scheme in order to achieve the scheme on (B), which the only difference is replacing yellow with white as the middle color. Does anyone have any idea on how to achieve that in Altair?
The color scheme on (B) was created in R, by using the RdYlBu color pallete (the one with 11 colors) and overwrite the middle (6th color) with white. Then, they increased the number of colors in the pallete to 99, to make the fade look more fluid.
My current code (A):
color=alt.Color('Spline_WW_Diff_Trend:Q', scale=alt.Scale(scheme='redyellowblue',reverse=True, domain=[-3.57,2.270], domainMid=0, clamp=True), legend=alt.Legend(title="Trend"))
I have tried manually setting up the colors using range but got an odd result. I've also used a condition to override the color for the value 0, but it wasn't satisfactory because the numbers neighboring 0 should have a white(ish) color.
Upvotes: 0
Views: 251
Reputation: 13880
You probably want interpolate='rgb'
when defining your own range
.Using the interpolate
property for the color scale you can define one of the interpolation methods as is defined by d3-interpolate, https://github.com/d3/d3-interpolate#color-spaces.
The default value for interpolate is hcl
, which is not always what you want. Observe the changes in color interpolation once you change the interpolation methods with a fixed range/domain:
import altair as alt
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': np.arange(-10, 10)})
def charter(method):
return alt.Chart(df, title=method).mark_rect().encode(
x=alt.X('x:O',title=None),
color=alt.Color('x:Q',
scale=alt.Scale(
domain=[-10,-5,0,5,9],
range=['red','orange','white','lightblue','darkblue'],
interpolate=method
),
legend=alt.Legend(direction='horizontal', orient='top', title=None)
)
)
methods = ['hcl', 'rgb', 'hsl', 'hsl-long', 'lab', 'hcl-long', 'cubehelix', 'cubehelix-long']
alt.vconcat(*[charter(method) for method in methods]).resolve_scale(color='independent')
Upvotes: 3