【问题标题】:Substring filtering in Altair / using "params"Altair 中的子字符串过滤/使用“参数”
【发布时间】:2022-01-03 20:50:09
【问题描述】:

我正在使用 Altair,并希望使用子字符串搜索过滤数据。 Here 是在 vega-lite 中执行此操作的示例。代码如下:

{
  "config": {"view": {"continuousWidth": 400, "continuousHeight": 300}},
  "data": {"name": "d"},
  "mark": "point",
  "encoding": {
    "x": {"type": "quantitative", "field": "xval", "scale":{"domain": [0,4]}},
    "y": {"type": "quantitative", "field": "yval", "scale":{"domain": [1,10]}}
  },
  "params": [{"name": "Letter", "value": "A", 
    "bind": {"input": "select", "options": ["A", "B", "C", "D", "E", "F"]}
  }],
  "transform": [
    {"filter": "indexof(datum.info, Letter)>-1"}
  ],
  "datasets": {
    "d": [
      {"xval": 1, "yval": 7, "info": "A;B;D;E"},
      {"xval": 2, "yval": 2, "info": "A;C;E;F"},
      {"xval": 3, "yval": 9, "info": "A;B;D"}
    ]
  }
}

这允许我过滤掉 info 列中包含“A”、“B”、“C”等的行,但它依赖于 "params",它在 Altair 中是 not available - 还有其他的吗到目前为止,如何在 Altair 中实现这种“子字符串”过滤?这只是一个最小的例子,但在我的实际用例中,我有大量的“选项”(许多基因名称),因此在原始数据中为每个选项添加一列是不可行的。

尝试在 Altair 中执行此操作,因为它是针对我认为允许 Altair 但不允许 vega-lite 的可执行研究文章。

【问题讨论】:

    标签: altair vega-lite


    【解决方案1】:

    编辑:意识到像infoSel.info[0] 这样的索引会从下拉列表中给出选择的字符串。这仍然适用于infoSel.info(没有索引),但这只是幸运 - 在这样的表达式中,infoSel.info[0] 更正确。

    知道了!这可以通过transform_filter 中的表达式实现,我之前尝试过但操作不正确(我使用的是下拉列表的名称,而不是选择对象的名称):

    d = pd.DataFrame({'xval': [1, 2, 3], 
                      'yval': [7, 2, 9], 
                      'info': ['A;B;D;E', 'A;C;E;F', 'B;D']})
    info_dropdown = alt.binding_select(options=['A', 'B', 'C', 'D', 'E', 'F'], name='Letter')
    info_sel = alt.selection_single(name='infoSel', fields=['info'], bind=info_dropdown, init={'info': 'A'}) 
    alt.Chart(d).mark_circle().encode(
        x='xval', y='yval'
        ).add_selection(info_sel).transform_filter('indexof(datum.info, infoSel.info[0])>-1')
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多