python - 在 altair 中修复刻度底部颜色为 0
问题描述
我正在通过以下方式生成华夫饼图(类似 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 始终位于刻度的底部?(透明色)
解决方案
您可以像设置domain
轴一样设置色标:scale=alt.Scale(range=["transparent", "green"], domain=[0, 16])
. 可以domainMin
在较新版本的 VegaLite 中进行设置,但在 Altair 中尚未设置。在您的情况下,无论如何设置最小值和最大值可能是一个好主意,以便所有年份的颜色都被解释为相同。
推荐阅读
- python - 如何更改 django inlineformset_factory 生成的表单集的外键列表?
- python - 如何在我的 python 脚本中包含密码学包,这样其他人就不必单独安装它?
- python - 如果使用python不存在特定(键:值),则尝试在集合(mongodb)中插入少量记录(键和值)
- dynamics-crm - 更改销售中心统一接口的 D365 CE 站点地图上“主页”链接的 URL
- android - Kivy - 音频管理
- javascript - 如何设置滚动焦点?
- python - Spacy Entity Ruler 正则表达式模式无法识别匹配
- forms - Blazor:通过反射生成和绑定输入控件
- java - 计算创建的对象数量的类 + 随机数生成器 Java
- flutter - 如何在画中画模式下进入jitsi会议室?