【问题标题】:Recreating the pyLDAvis chart in Altair - filtered data with empty selection在 Altair 中重新创建 pyLDAvis 图表 - 使用空选择过滤数据
【发布时间】:2021-06-11 00:12:50
【问题描述】:

我正在尝试为 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"
)

【问题讨论】:

    标签: python topic-modeling altair vega-lite pyldavis


    【解决方案1】:

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

    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"
    )
    
    

    我相信这可以解决您提到的两个问题。还有第三个问题,即条形不像您的示例那样动态使用,但我不确定如何解决。

    【讨论】:

    • 非常感谢乔尔!这很棒。 wrt 排序,看起来杰克的这个答案很接近,使用transform_calculate。我正在研究它,但还无法推断出我的特定数据。你有什么想法吗? stackoverflow.com/a/64632726
    • @campo 我不确定,但很有趣,所以I asked that follow up questionhere。我认为您可以将此标记为已解决并将该问题添加为书签,以查看何时/是否有人回复。
    • 非常感谢@joelostblom。非常感谢你发布一个新问题!我很感激。
    猜你喜欢
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多