在 Altair 中重新创建 pyLDAvis 图表 - 使用空选择过滤数据

Recreating the pyLDAvis chart in Altair - filtered data with empty selection

我正在尝试为 Altair 中的主题建模重新创建经典的 pyLDAvis 可视化。

我在过滤方面遇到了麻烦。在 pyLDAvis 图表中,散点图中的空选择显示右图中所谓的“默认”主题,它仅显示语料库中每个单词的总频率。

另一方面,如果您在散点图中进行选择,条形图会被过滤,以便显示所选内容的总计,叠加在总计上,如下所示:

我可以接近这个,但是正如你在下面看到的,有(至少)两个不同点:

有谁知道我如何根据以上问题更接近?也就是说,我想在没有选择时仅显示总计,并在单击某个点时将选择与总计叠加。

以下可重现的 Altair 代码:

import altair as alt
import pandas as pd

data={
 'Term': ['algorithm','learning','learning','algorithm','algorithm','learning'],
 'Freq_x': [1330,1353,304.42,296.69,157.59,140.35],
 'Total': [1330, 1353,1353.7,1330.47,1330.47,1353.7],
 'Category': ['Default', 'Default', 'Topic1', 'Topic1', 'Topic2', 'Topic2'],
 'logprob': [30.0, 27.0, -5.116, -5.1418, -5.4112, -5.5271],
 'loglift': [30.0, 27.0, 0.0975, 0.0891, -0.1803, -0.3135],
 'saliency_ind': [0, 3, 76, 77, 181, 186],
 'x': [nan,nan,-0.0080,-0.0080,-0.0053,-0.0053],
 'y': [nan,nan,-0.0056,-0.0056, 0.0003,0.0003],
 'topics': [nan, nan, 1.0, 1.0, 2.0, 2.0],
 'cluster': [nan, nan, 1.0, 1.0, 1.0, 1.0],
 'Freq_y': [nan,nan,20.39,20.39,14.18,14.18]}

df=pd.DataFrame(data)

pts = alt.selection(type="single", fields=['Category'])

points=alt.Chart().mark_circle(tooltip=True).encode(
    x='mean(x)',
    y='mean(y)',
    size='Freq_y',
    tooltip=['topics', 'cluster'],
    color=alt.condition(pts, "Category", alt.ColorValue("grey"))
).add_selection(pts)

bars=alt.Chart().mark_bar().encode(
    x='Freq_x',
    y=alt.Y('Term', sort=alt.SortField("Freq_x", order='descending')),
    tooltip=['Total'],
    color='Category'
).transform_filter(
    pts
)

alt.hconcat(points,bars, data=df).resolve_legend(
    color="independent",
    size="independent"
)

您可以在第一个条形图上叠加一个单独的条形图,并且只在这个叠加图上使用变换过滤器。要在开始时不显示任何段,您可以设置选择的空行为。

import altair as alt
import pandas as pd


# I modified these values slightly
data={
 'Term': ['algorithm','learning','learning','algorithm','algorithm','learning'],
 'Freq_x': [1330,1153,504.42,296.69,177.59,140.35],
 'Total': [1330, 1353,1353.7,1330.47,1330.47,1353.7],
 'Category': ['Default', 'Default', 'Topic1', 'Topic1', 'Topic2', 'Topic2'],
 'logprob': [30.0, 27.0, -5.116, -5.1418, -5.4112, -5.5271],
 'loglift': [30.0, 27.0, 0.0975, 0.0891, -0.1803, -0.3135],
 'saliency_ind': [0, 3, 76, 77, 181, 186],
 'x': [None,None,-0.0080,-0.0080,-0.0053,-0.0053],
 'y': [None,None,-0.0056,-0.0056, 0.0003,0.0003],
 'topics': [None,None, 1.0, 1.0, 2.0, 2.0],
 'cluster': [None,None, 1.0, 1.0, 1.0, 1.0],
 'Freq_y': [None,None,20.39,20.39,14.18,14.18]}

df=pd.DataFrame(data)

pts = alt.selection(type="single", fields=['Category'], empty='none')

points=alt.Chart().mark_circle(tooltip=True).encode(
    x='mean(x)',
    y='mean(y)',
    size='Freq_y',
    tooltip=['topics', 'cluster'],
    detail='Category',
    color=alt.condition(pts, alt.value('#F28E2B'), alt.value('#4E79A7'))
).add_selection(pts)

bars=alt.Chart().mark_bar().encode(
    x='Freq_x',
    y=alt.Y('Term', sort='-x'),
    tooltip=['Total'],
)

bars2=alt.Chart().mark_bar(color='#F28E2B').encode(
    x='Freq_x',
    y=alt.Y('Term', sort='-x'),
    tooltip=['Freq_x'],
).transform_filter(
    pts
)

alt.hconcat(points,bars+bars2, data=df).resolve_legend(
    color="independent",
    size="independent"
)

我相信这可以解决您提到的两个问题。还有第三个,就是条形图不像你的例子那样动态求助,但我不知道如何解决。