使用选择时 Altair 工具提示不起作用
Altair tooltips don't work when using selection
我正在尝试使用类似于此处 https://altair-viz.github.io/gallery/multiline_tooltip.html 的 altair 多行工具提示示例中显示的垂直规则。我也想使用文本工具提示,因为我的数据集包含与每个点相关的文本注释。
这里是示例代码,我在其中添加了一个带有虚拟文本的“评论”列,我还在行组件中添加了工具提示,但工具提示不起作用。
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
我添加了这一行来创建评论栏:
source['comments']='comment'+source.category+source.x.apply(str)
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
# The basic line
我在这里添加了工具提示:
line = alt.Chart(source).mark_line(interpolate='basis').encode(
x='x:Q',
y='y:Q',
color='category:N',tooltip='comments'
)
其余代码:
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
)
# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
x='x:Q',
).transform_filter(
nearest
)
# Put the five layers into a chart and bind the data
alt.layer(
line, selectors, points, rules, text
).properties(
width=600, height=300
)
如果您 运行 此代码,它不会显示工具提示。要查看工具提示,我必须摆脱选择,因此要在最后显示图表并查看工具提示,而不是规则,我必须这样做:
alt.layer(line).properties(width=600, height=300)
如何同时拥有工具提示和规则?
这是 Vega-Lite 中的一个错误;参见 vega-lite#5732。
为了解决这个问题,您可以为每个尚无选区的图层添加一个选区;例如
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
).add_selection(alt.selection_single())
那么无论图层是单独显示还是在分层图表中显示,您的工具提示的行为都是一样的。
半相关说明:将工具提示放在折线图上可能不是您想要的(它会为每个线标记生成一个工具提示)。尝试将工具提示放在规则标记上;有关此类工具提示的示例,请参阅 Pivot Transform 示例。
我正在尝试使用类似于此处 https://altair-viz.github.io/gallery/multiline_tooltip.html 的 altair 多行工具提示示例中显示的垂直规则。我也想使用文本工具提示,因为我的数据集包含与每个点相关的文本注释。
这里是示例代码,我在其中添加了一个带有虚拟文本的“评论”列,我还在行组件中添加了工具提示,但工具提示不起作用。
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
我添加了这一行来创建评论栏:
source['comments']='comment'+source.category+source.x.apply(str)
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
# The basic line
我在这里添加了工具提示:
line = alt.Chart(source).mark_line(interpolate='basis').encode(
x='x:Q',
y='y:Q',
color='category:N',tooltip='comments'
)
其余代码:
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
)
# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
x='x:Q',
).transform_filter(
nearest
)
# Put the five layers into a chart and bind the data
alt.layer(
line, selectors, points, rules, text
).properties(
width=600, height=300
)
如果您 运行 此代码,它不会显示工具提示。要查看工具提示,我必须摆脱选择,因此要在最后显示图表并查看工具提示,而不是规则,我必须这样做:
alt.layer(line).properties(width=600, height=300)
如何同时拥有工具提示和规则?
这是 Vega-Lite 中的一个错误;参见 vega-lite#5732。
为了解决这个问题,您可以为每个尚无选区的图层添加一个选区;例如
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
).add_selection(alt.selection_single())
那么无论图层是单独显示还是在分层图表中显示,您的工具提示的行为都是一样的。
半相关说明:将工具提示放在折线图上可能不是您想要的(它会为每个线标记生成一个工具提示)。尝试将工具提示放在规则标记上;有关此类工具提示的示例,请参阅 Pivot Transform 示例。