【发布时间】:2020-12-16 20:18:41
【问题描述】:
我有一个 Altair 图表,类似于下面代码生成的图表。在选择任何数据之前,我可以在线图上看到x 和t 的标签(下面的第一张图片)。但是,选择数据后,数字会出现在轴上并推出标签(下图第二张)。如何不让标签被切断?在我的实际问题中,x 轴和 y 轴的比例在选择之间可能会有很大差异,因此不能对所有可能的选择使用比例。
import altair as alt
import numpy as np
import pandas as pd
n_freq = 5
n_data = 1000
freqs = np.arange(1, n_freq + 1)
df_freq = pd.DataFrame({'f': freqs})
df_data = pd.DataFrame({
'f': np.repeat(a=freqs, repeats=n_data),
't': np.tile(A=np.linspace(0, 2, n_data), reps=n_freq),
})
df_data['x'] = np.sin(2 * np.pi * df_data['f'] * df_data['t'])
freq_selection = alt.selection_single(
fields=('f',),
empty='none',
on='mouseover',
nearest=True,
clear='mouseout',
)
freq_table = alt.Chart(df_freq).mark_text().encode(
y=alt.Y(shorthand='f:Q', axis=None, scale=alt.Scale(domain=(0, n_freq + 1))),
text='f:N',
).add_selection(freq_selection)
data_plot = alt.Chart(df_data).mark_line().encode(
x='t:Q',
y='x:Q',
).transform_filter(freq_selection)
alt.hconcat(freq_table, data_plot, spacing=100).properties(padding=100)
选择之前:
选择后:
【问题讨论】:
标签: altair