我正在使用以下代码生成一个绘图(在 ipython 笔记本中):
import altair as alt
import pandas as pd
events = pd.DataFrame(
[
{"event": "Task A", "equipment": "SK-101", "start": 10.2, "finish": 11.3},
{"event": "Task B", "equipment": "SK-102", "start": 6.5, "finish": 10.2},
{"event": "Task C", "equipment": "SK-103", "start": 3.3, "finish": 11.3},
{"event": "Task D", "equipment": "SK-104", "start": 4.7, "finish": 5.5},
{"event": "Task E", "equipment": "SK-105", "start": 13.0, "finish": 13.2},
{"event": "Task F", "equipment": "SK-106", "start": 1.1, "finish": 7.9},
{"event": "Task G", "equipment": "SK-107", "start": 7.4, "finish": 10.0},
{"event": "Task H", "equipment": "SK-108", "start": 6.6, "finish": 7.6},
{"event": "Task I", "equipment": "SK-109", "start": 8.5, "finish": 16.7},
{"event": "Task J", "equipment": "SK-110", "start": 9.0, "finish": 12.2},
{"event": "Task K", "equipment": "SK-111", "start": 10.2, "finish": 17.3},
{"event": "Task L", "equipment": "SK-112", "start": 6.1, "finish": 9.5},
{"event": "Task M", "equipment": "SK-113", "start": 5.4, "finish": 5.8},
{"event": "Task N", "equipment": "SK-114", "start": 2.2, "finish": 8.3},
{"event": "Task O", "equipment": "SK-115", "start": 9.3, "finish": 10.6},
{"event": "Task P", "equipment": "SK-116", "start": 3.9, "finish": 12.5},
{"event": "Task Q", "equipment": "SK-117", "start": 11.1, "finish": 16.6},
{"event": "Task R", "equipment": "SK-118", "start": 14.4, "finish": 18.4},
{"event": "Task S", "equipment": "SK-119", "start": 19.2, "finish": 19.9},
{"event": "Task T", "equipment": "SK-120", "start": 13.8, "finish": 16.7},
{"event": "Task U", "equipment": "SK-102", "start": 12.0, "finish": 13.0},
{"event": "Task V", "equipment": "SK-106", "start": 10.2, "finish": 17.3},
{"event": "Task W", "equipment": "SK-108", "start": 12.8, "finish": 14.9},
{"event": "Task X", "equipment": "SK-110", "start": 12.6, "finish": 18.9},
{"event": "Task Y", "equipment": "SK-112", "start": 13.3, "finish": 18.3},
{"event": "Task Z", "equipment": "SK-114", "start": 8.6, "finish": 19.2},
]
)
brush = alt.selection_interval(encodings=["y"])
minimap = (
alt.Chart(events)
.mark_bar()
.add_params(brush)
.encode(
x=alt.X("start:Q", title="", axis=alt.Axis(labels=False, tickSize=0)),
x2="finish",
y=alt.Y("equipment:N", title="", axis=alt.Axis(labels=False, tickSize=0)),
color=alt.condition(brush, "event", alt.value("lightgray")),
)
.properties(
width=100,
height=400,
title="minimap"
)
)
detail = (
alt.Chart(events)
.mark_bar()
.encode(
x=alt.X("start:Q", title="time (hr)"),
x2="finish",
y=alt.Y("equipment:N").scale(domain={"param": brush.name, "encoding": "y"}),
color="event",
)
.properties(width=600, height=400, title="Equipment Schedule")
)
detail | minimap
这个想法是,右侧的小地图用于缩放/平移主图的 y 轴。 当我在小地图中用鼠标缩放或平移时,我在主图的顶部看到了奇怪的文物;看起来所选区域上方的所有条形都渲染在绘图的顶部。
这是我做错了什么,还是某种渲染错误?
我不确定为什么会发生这种情况,但另一种方法是使用画笔作为过滤器而不是设置 y 域。只要您能够设置固定的 x 域,我认为这可以很好地满足您的需求:
detail = (
alt.Chart(events)
.mark_bar()
.encode(
x=alt.X("start:Q", title="time (hr)").scale(domain=(0, 20)),
x2="finish",
y=alt.Y("equipment:N"),
color="event",
)
.properties(width=600, height=alt.Step(20), title="Equipment Schedule")
).transform_filter(brush)
我还在此处将高度设置为步长,这为您提供了更多您在其他问题中提到的滚动行为。