簡體   English   中英

嘗試將 Altair 顏色設置為零值的白色

[英]Trying to set Altair color to white at zero value

我正在嘗試創建一個 Altair 熱圖,其中當我的計數為零時顏色為白色。

這是我的數據框:

df11 = pd.DataFrame({
    'lineid': [0,1,2,0,1,2],
    'term': ['data', 'data', 'data', 'explore', 'explore', 'explore'],
    'count': [3,2,1,1,0,2],
    'title': ['weather', 'weather','weather','weather','weather','weather',]
})
alt.Chart(d11).mark_rect().encode(
    x=alt.X('lineid:O', title=None, axis=alt.Axis(ticks=False, labels=False)),
    y=alt.Y('term:O', title=None),
    color=alt.Color('count:O', legend=None)
)

編碼給了我這個圖表(這是我想要的)但我試圖讓零計數(第 2 行,第 2 列)是白色而不是淺藍色。

在此處輸入圖像描述

您可以使用條件編碼,如How to make Altair display NaN points with a quantitative color scale?

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'lineid': [0,1,2,0,1,2],
    'term': ['data', 'data', 'data', 'explore', 'explore', 'explore'],
    'count': [3,2,1,1,0,2],
    'title': ['weather', 'weather','weather','weather','weather','weather',]
})
alt.Chart(df).mark_rect().encode(
    x=alt.X('lineid:O', title=None, axis=alt.Axis(ticks=False, labels=False)),
    y=alt.Y('term:O', title=None),
    color=alt.condition(alt.datum.count == 0, alt.value('white'), 'count:O', legend=None)
)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM