【发布时间】:2020-11-30 09:27:16
【问题描述】:
我正在尝试为 Altair 中的多面图表设置多行工具提示。该构面有助于比较多年数据(即,可以轻松地将 2018 年 1 月与 2019 年 1 月进行比较)。
以下是包含随机数据的代码示例,遵循文档中的示例:https://altair-viz.github.io/gallery/multiline_tooltip.html。
根据我定义方面的位置,我会遇到以下错误之一:
-
AttributeError: 'FacetChart' object has no attribute 'mark_point':当我在开始时对折线图进行刻面,然后尝试在此折线图上应用 mark_point() 时发生 -
ValueError: Faceted charts cannot be layered.: 当我最终刻面折线图时发生(在构建图层时)
有解决办法吗?
start = pd.Timestamp('20180101', tz='Europe/Rome')
end = pd.Timestamp('20200101', tz='Europe/Rome')
index = pd.date_range(start,end,freq='M')[:-1]
source = pd.DataFrame(np.cumsum(np.random.randn(len(index), 3), 0).round(2),
columns=['A', 'B', 'C'], index=index)
source.index.names = ['x']
source = source.reset_index().melt('x', var_name='category', value_name='y')
### line chart with facet
line = alt.Chart(source).mark_line(point=True).encode(alt.X('month(x):O', title=''), alt.Y('y:Q', title=''), color='category:N').facet(row=alt.Row('year(x):O', title=''))
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
selectors = alt.Chart(source).mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
points = line.mark_point().encode(
opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)
text = line.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 'y:Q', alt.value(' '))
)
rules = alt.Chart(source).mark_rule(color='gray').encode(
x='x:Q',
).transform_filter(
nearest
)
alt.layer(
line, selectors, points, rules, text
).properties(
width=600, height=300
)
【问题讨论】: