【发布时间】:2021-07-10 15:51:37
【问题描述】:
我正在通过以下方式生成华夫饼图(类似 github 的活动热图):
import altair as alt
import pandas as pd
# Import data
df = pd.read_csv("https://pastebin.com/raw/AzwJ0va4")
# Year interactive dropdown
years = list(df["year"].unique())
year_dropdown = alt.binding_select(options=years)
selection = alt.selection_single(
fields=["year"], bind=year_dropdown, name="Year", init={"year": 2020}
)
# Plot
(
alt.Chart(df)
.mark_rect()
.encode(
x=alt.X("week:O", title="Week"),
y=alt.Y("day(committed_on):O", title=""),
color=alt.Color(
"hash:Q", scale=alt.Scale(range=["transparent", "green"]), title="Commits"
),
tooltip=[
alt.Tooltip("committed_on", title="Date"),
alt.Tooltip("day(committed_on)", title="Day"),
alt.Tooltip("hash", title="Commits"),
],
)
.add_selection(selection)
.transform_filter(selection)
.properties(width=1000, height=200)
)
结果图的行为符合我的预期,达到 99%,但是当我选择没有活动的年份(hash 列填充为 0)作为 2017 时,该图将填充0 的绿色方块正好锚定在刻度的中间。
如何确保 0 始终位于刻度的底部? (透明色)
【问题讨论】: