简体   繁体   English

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

[英]Altair : Make Interval Selection Line plot with dual axis and datetime x axis

I am trying to make a dual y axis line plot with date time index as x axis, with interval selection, so far:到目前为止,我正在尝试制作一条双 y 轴线 plot,日期时间索引为 x 轴,间隔选择:

#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

If I try to produce p I am getting the following error:如果我尝试生成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

If I try to produce upper I get:如果我尝试生产鞋面,我会得到:

在此处输入图像描述

My dataframe:我的 dataframe:

在此处输入图像描述

Any idea how can I get interval selection here?知道如何在这里获得间隔选择吗?

Also I keep getting this error while working with Altair, any idea how can I debug this??此外,我在使用 Altair 时不断收到此错误,知道如何调试吗? I have no experience in java, and am using a Mac.我在 java 方面没有经验,并且正在使用 Mac。

EDIT编辑

After adding the selection to only one of the subplots,仅将选择添加到其中一个子图后,

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

I am getting this:我得到这个:

在此处输入图像描述

(1) The selection isn't working (1) 选择无效

(2) And somehow i am getting an exact replica of upper for the lower plot (2) 不知何故,我得到了upper plot 的精确复制品

**EDIT -- This made it work ** **编辑——这使它工作**

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

在此处输入图像描述

Add the selection to just px or py instead.将选择添加到pxpy If you add the same selection to both subcharts in a layer, it results in this error.如果您将相同的选择添加到图层中的两个子图表,则会导致此错误。 This is a bug that should probably be fixed in Altair.这是一个应该在 Altair 中修复的错误。

To be more concrete, your code should look like this:更具体地说,您的代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM