Python 牵牛星:如何在烛台图(即:分层图)上使用区间选择

Python 牵牛星:如何在烛台图(即:分层图)上使用区间选择,python,altair,Python,Altair,我试图复制以下堆叠图表,顶部图表的域可以通过与底部图表交互来选择,但要使用烛台图表(这是一个分层图表): (下面的示例代码如下:) 我无法让它与分层图表一起工作()。有没有可能用分层图表的方式按日期删除?谢谢 这就是我尝试过的: import altair as alt from vega_datasets import data source = data.ohlc() brush = alt.selection(type='interval', encodings=['x']) open

我试图复制以下堆叠图表,顶部图表的域可以通过与底部图表交互来选择,但要使用烛台图表(这是一个分层图表):

(下面的示例代码如下:)

我无法让它与分层图表一起工作()。有没有可能用分层图表的方式按日期删除?谢谢

这就是我尝试过的:

import altair as alt
from vega_datasets import data

source = data.ohlc()
brush = alt.selection(type='interval', encodings=['x'])
open_close_color = alt.condition("datum.open <= datum.close", alt.value("green"), alt.value("firebrick"))

base = alt.Chart(source).encode(
    alt.X('date:T', axis=alt.Axis(format='%m/%d', title='Date in 2009')),
    color=open_close_color
).properties(width=600, height=400)

rule = base.mark_rule().encode(
    alt.Y('low:Q', title='Price', scale=alt.Scale(domain=brush, zero=False)),
    alt.Y2('high:Q')
)

bar = base.mark_bar().encode(
    alt.Y('open:Q', scale=alt.Scale(domain=brush, zero=False)),
    alt.Y2('close:Q')
)

candles = rule + bar

lower = base.properties(
    height=60
).add_selection(brush)

candles & lower
将altair导入为alt
从vega_数据集导入数据
source=data.ohlc()
brush=alt.selection(type='interval',encodings=['x'])

open_close_color=alt.condition(“datum.open这是通过Vega编辑器生成的Vega Lite规范和交互式图表:

这是使用以下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
将altair导入为alt
从vega_数据集导入数据
source=data.ohlc()

open\u close\u color=alt.condition(“datum.open非常感谢您的回答。我只使用了牵牛星(不是织女星)--那么我需要安装Vega并将dict传递给它以进行绘图吗?TX你想对绘图做什么?如果你想让它在Altair中交互,我不确定什么会起作用。我认为Altair使用的Vega Lite版本存在跨层选择问题。如果你想从Vega Lite获得绘图,你可以使用我链接到的Vega Lite编辑器,或者你可以使用一些Vega嵌入代码将其嵌入到网页中。注意:使用Vega嵌入代码,我将Vega Lite的版本更改为4.8.1,交互性停止工作。Altair本质上是Vega Lite的包装器。它创建了一个Vega Lite json规范。我曾用它来获取规范,但我发现了一个更好的way with print((上下)。to_json(indent=2))我想我最终会输出到一个网页,所以我会查看你的vega嵌入链接。再次感谢。