【问题标题】:Altair: Use of color scheme with log scalesAltair:使用对数刻度的配色方案
【发布时间】:2019-03-25 04:00:10
【问题描述】:

我是 Altair 的新手,我正在尝试制作一个带有对数刻度颜色的热图,并且还选择了一个非默认配色方案(默认方案使用的范围非常小,而且我想要 light-to -暗色)。我发现使用type=log 可以轻松获得对数刻度,但是一旦这样做,scheme= 参数就会被忽略。如果我改为使用range= 手动设置高低颜色,则效果很好。

我进一步发现,如果我以任何方式显式设置type=,即使显式设置默认设置type='linear'scheme= 也会被忽略。这是一个错误吗?如果不是,我如何以一种有意义的方式理解配色方案的使用?如果我不能直接使用方案,如何检查方案并提取其颜色值以重复使用?

这里有一些例子:

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

【问题讨论】:

    标签: python numpy altair


    【解决方案1】:

    我认为这一定是一个错误。我在 github 上找不到修复此行为的问题,但您发布的代码现在似乎按预期工作。我正在运行 atair 版本'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'))
    )
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 2021-05-12
      • 2023-01-27
      • 1970-01-01
      • 2021-01-23
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多