以下是通过 Vega 编辑器提供的 Vega-Lite 规格和交互式图表:https://vega.github.io/editor/#/gist/2e5d94af2d36ee770b980f648774b865/vega-lite-interactive-candlestick-spec.json https://vega.github.io/editor/#/gist/2e5d94af2d36ee770b980f648774b865/vega-lite-interactive-candlestick-spec.json
这是使用以下 python/altair 代码创建的:
import altair as alt
from vega_datasets import data
source = data.ohlc()
open_close_color = alt.condition("datum.open <= datum.close",
alt.value("#06982d"),
alt.value("#ae1325"))
brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(source).encode(
alt.X('date:T',
axis=alt.Axis(
format='%m/%d',
labelAngle=-45,
title='Date in 2009'
)
),
color=open_close_color
).properties(
width=600,
)
rule = base.mark_rule().encode(
alt.Y(
'low:Q',
title='Price',
scale=alt.Scale(zero=False),
),
alt.Y2('high:Q')
)
bar = base.mark_bar().encode(
alt.Y('open:Q'),
alt.Y2('close:Q')
)
lower = alt.layer(rule, bar, height=60).add_selection(brush)
upper = (rule + bar).encode(
alt.X('date:T', scale=alt.Scale(domain=brush))
)
# (upper & lower) # display the charts
print((upper & lower).to_json(indent=2)) # get the vega-lite spec
我在 Jupyter 设置中的交互性方面遇到了一些问题,但是当我将规范复制到 Vega 编辑器中时,它就可以工作了。这可能与 Vega-Lite 的版本有关(我的设置为 4.8.1,Vega 编辑器为 4.16.7)。