Python 牵牛星:使用双轴和日期时间x轴绘制间隔选择线图

Python 牵牛星:使用双轴和日期时间x轴绘制间隔选择线图,python,visualization,altair,Python,Visualization,Altair,到目前为止,我正在尝试用日期-时间索引作为x轴绘制双y轴线图,并选择了间隔: #base encoding the X-axis brush = alt.selection(type='interval', encodings=['x']) base = alt.Chart(yData.reset_index()) base = base.encode(alt.X('{0}:T'.format(yData.index.name), axis=a

到目前为止,我正在尝试用日期-时间索引作为x轴绘制双y轴线图,并选择了间隔:

#base encoding the X-axis
brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(yData.reset_index())
base = base.encode(alt.X('{0}:T'.format(yData.index.name), 
                         axis=alt.Axis(title=yData.index.name)))

py = base.mark_line()
py = py.encode(alt.X('date:T',scale = alt.Scale(domain = brush)),
               alt.Y('plotY', axis=alt.Axis(title='ylabel1')))
py = py.properties(width = 700, height = 230)


px = base.mark_line()
px = px.encode(alt.Y('plotX1', axis=alt.Axis(title='ylabel2')))
px = px.properties(width = 700, height = 230)

upper = (py + px).resolve_scale(y='independent')

lower = upper.copy()
lower = lower.properties(height=20).add_selection(brush)

p = alt.vconcat(upper, lower).configure_concat(spacing=0)
p
如果我试图产生p我会得到以下错误:

Javascript Error: Duplicate signal name: "selector045_x"
This usually means there's a typo in your chart specification. See the JavaScript console for the full traceback
如果我尝试制作鞋面,我会得到:

我的数据帧:

你知道我如何在这里选择时间间隔吗

我在使用Altair时也经常遇到这个错误,你知道我该如何调试它吗??我没有java方面的经验,正在使用Mac

编辑

仅将选择添加到其中一个子批次后

brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(yData.reset_index())
base = base.encode(alt.X('{0}:T'.format(yData.index.name), 
                         axis=alt.Axis(title=yData.index.name)))

py = base.mark_line()
py = py.encode(alt.X('date:T',scale = alt.Scale(domain = brush)),
               alt.Y('plotY', axis=alt.Axis(title='ylabel1')))
py = py.properties(width = 700, height = 230).add_selection(brush)


px = base.mark_line()
px = px.encode(alt.Y('plotX1', axis=alt.Axis(title='ylabel2')))
px = px.properties(width = 700, height = 230)

upper = (py + px).resolve_scale(y='independent')

lower = upper.copy()
lower = lower.properties(height=20)

p = alt.vconcat(upper, lower).configure_concat(spacing=0)
p
我明白了:

(1) 选择无效

(2) 不知何故,我得到了一个精确的
上部
副本,用于下部的绘图

**编辑--这使它工作了**

brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(yData.reset_index())
base = base.encode(alt.X('{0}:T'.format(yData.index.name), 
                         axis=alt.Axis(title=yData.index.name)))

py = base.mark_line(color='orange')
py = py.encode(alt.X('date:T',scale = alt.Scale(domain = brush)),
               alt.Y('plotY', axis=alt.Axis(title='ylabel1')),
              )
py = py.properties(width = 700, height = 230)


px = base.mark_line()
px = px.encode(alt.X('date:T',scale = alt.Scale(domain = brush)),
               alt.Y('plotX1', axis=alt.Axis(title='ylabel2')))
px = px.properties(width = 700, height = 230)

# upper = px
upper = (py + px).resolve_scale(y='independent')
lower = px.copy()
lower = lower.properties(height=20).add_selection(brush)

p = alt.vconcat(upper, lower).configure_concat(spacing=0)
p

只将选择添加到
px
py
。如果将相同的选择添加到图层中的两个子图形,则会导致此错误。这是一个应该在Altair中修复的bug

更具体地说,您的代码应该如下所示:

# ...
px = px.properties(width = 700, height = 230).add_selection(brush)  # add selection here
# ...
lower = lower.properties(height=20)  # not here
# ...

将所选内容添加到
px
py
。如果将相同的选择添加到图层中的两个子图形,则会导致此错误。这是一个应该在Altair中修复的bug

更具体地说,您的代码应该如下所示:

# ...
px = px.properties(width = 700, height = 230).add_selection(brush)  # add selection here
# ...
lower = lower.properties(height=20)  # not here
# ...

你的意思是不是说,不使用
lower.properties(高度=20)。添加选择(画笔)
??我无法理解..,我只在一个子批次中使用了
scale
作为响应。但它仍然没有解决这个问题。我更新了我的问题。同样,我可以通过将
lower=upper.copy()
更改为
lower=px.copy()
来解决“副本”部分。但是
lower
中的选择仍然没有在
upper
绘图中进行选择问题在于您已经在px中设置了域,该域位于下方和上方的图表中。无法使用绑定到该子图表的选择设置子图表的比例(如果选择框的比例随着该选择框的更改而更改,则会出现循环和定义错误)。您需要做的是重新构造图表,以便只在下方的图表中调用
add_selection
,并且只在上方的图表中添加
alt.Scale(domain=…)
。您的意思是不是要取代
lower.properties(height=20)。添加_selection(brush)
??我无法理解..,我只在一个子批次中使用了
scale
作为响应。但它仍然没有解决这个问题。我更新了我的问题。同样,我可以通过将
lower=upper.copy()
更改为
lower=px.copy()
来解决“副本”部分。但是
lower
中的选择仍然没有在
upper
绘图中进行选择问题在于您已经在px中设置了域,该域位于下方和上方的图表中。无法使用绑定到该子图表的选择设置子图表的比例(如果选择框的比例随着该选择框的更改而更改,则会出现循环和定义错误)。您需要做的是重新构造图表,使
add\u selection
仅在下部图表中调用,而
alt.Scale(domain=…)
仅在上部图表中添加。