Altair:使用双轴和日期时间x轴制作间隔选择线图

时间:2019-11-16 19:59:22

标签: 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=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

如果我尝试产生 ,我会得到:

enter image description here

我的数据框:

enter image description here

有什么主意如何在这里获得间隔选择?

在使用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

我得到了:

enter image description here

(1)选择不起作用

(2)而且我以某种方式为下图获得了upper的精确副本

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

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

enter image description here

1 个答案:

答案 0 :(得分:2)

将选择内容仅添加到pxpy中。如果将同一选择添加到图层的两个子图中,则会导致此错误。这个错误可能应该在Altair中修复。

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

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