【问题标题】:Altair: Discrete/Binned Color Scheme for HeatmapAltair:热图的离散/分档配色方案
【发布时间】:2022-09-23 06:46:53
【问题描述】:

我正在尝试创建一个色阶是离散的而不是连续的热图。我想使用 5 种颜色,所以我认为条件语句行不通。我在Google Groups 上找到了这个例子,但它似乎没有完全工作。如何修复它以使实际热图按预期显示?

   import altair as alt
    import pandas as pd
    import numpy as np
    
    np.random.seed(0)
    df = pd.DataFrame({
        \'x\': np.random.randint(0, 5, 100),
        \'y\': np.random.randint(0, 5, 100),
        \'z\': np.random.rand(100)
    })
    
    alt.Chart(df).mark_rect().encode(
        x=\'x:O\',
        y=\'y:O\',
        color=alt.Color(\'mean(z)\', bin=alt.Bin(maxbins=5))
    )

预期结果:

实际结果:

    标签: colors legend heatmap altair binning


    【解决方案1】:

    基于https://vega.github.io/vega-lite/docs/scale.html#bin-ordinal,我认为你做的事情是正确的。在 Altair 的最新开发版本上运行您的代码会给我预期的结果,因此它可能是以前版本中的错误。您可以尝试直接从 GitHub 安装最新版本。

    相关:Altair Color binned values

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2022-01-14
      相关资源
      最近更新 更多