Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altair: Use of color scheme with log scales

I'm new to Altair and I'm trying to make a heatmap with a log scale for color, and also select a non-default color scheme (the default scheme uses very little extent, and also I want light-to-dark colors). I find that I can easily get a log scale with type=log, but that once I do that, the scheme= parameter is then ignored. If I instead manually set the high and low colors with range=, that works fine.

I further found that if I explicitly set type= in any way, even explicitly setting type='linear', which is the default, scheme= is then ignored. Is this a bug? If not, how can I understand the use of color schemes in a way which allows this to make sense? If I cannot directly use a scheme, how can I inspect the scheme and pull out its color values to reuse?

Here are some examples:

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

# This question is about Altair - plotnine is only here for the example data
from plotnine.data import diamonds

# This works, and gives me the greenblue color scheme:
alt.Chart(diamonds).mark_rect().encode(
    x=alt.X('carat',bin=True),
    y=alt.Y('price',bin=True),
    color=alt.Color('count()',scale=alt.Scale(scheme='greenblue'))
)

# This gives me a log scale, but now the greenblue scheme is gone:
alt.Chart(diamonds).mark_rect().encode(
    x=alt.X('carat',bin=True),
    y=alt.Y('price',bin=True),
    color=alt.Color('count()',scale=alt.Scale(type='log',scheme='greenblue'))
)

# Direct specification of range works, but it is not exactly the same
# colors as greenblue.  If this is the only way to do it, how do I open
# up the greenblue scheme and grab its colors?
alt.Chart(diamonds).mark_rect().encode(
    x=alt.X('carat',bin=True),
    y=alt.Y('price',bin=True),
    color=alt.Color('count()',scale=alt.Scale(type='log',range=['palegreen','blue']))
)
like image 532
Terran Melconian Avatar asked Oct 20 '18 13:10

Terran Melconian


1 Answers

I think this must have been a bug. I can't find the the issue on github where this behavior was fixed but the code you have posted appears to be working as expected now. I am running atair version '3.2.0'.

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

from plotnine.data import diamonds

# Added to alleviate the large dataset issues  
alt.data_transformers.enable('json')

alt.Chart(diamonds).mark_rect().encode(
    x=alt.X('carat',bin=True),
    y=alt.Y('price',bin=True),
    color=alt.Color('count()',scale=alt.Scale(scheme='greenblue'))
)

enter image description here

alt.Chart(diamonds).mark_rect().encode(
    x=alt.X('carat',bin=True),
    y=alt.Y('price',bin=True),
    color=alt.Color('count()',scale=alt.Scale(type='log',scheme='greenblue'))
)

enter image description here

like image 138
eitanlees Avatar answered Oct 19 '22 04:10

eitanlees