【问题标题】:Altair: Line Chart with Stroked Point MarkersAltair:带有笔划点标记的折线图
【发布时间】:2020-07-05 08:43:46
【问题描述】:

我正在尝试在 Altair 中创建带有点标记的折线图。我正在使用 Altair 文档中的 multi-series line chart example 并尝试将其与 Vega-Lite 文档中的 line chart with stroked point markers example 结合使用。

我感到困惑的是如何处理“mark_line”参数。从 Vega 示例中,我需要使用“点”,然后将“填充”设置为 False。

  "mark": {
    "type": "line",
    "point": {
      "filled": false,
      "fill": "white"
    }
  },

如何在 Altair 中应用它?我发现将 'point' 设置为 'True' 或 '{}' 添加了一个点标记,但对如何让填充起作用感到困惑。

source = data.stocks()

alt.Chart(source).mark_line(
    point=True
).encode(
    x='date',
    y='price',
    color='symbol'
)

【问题讨论】:

    标签: python data-visualization vega-lite altair


    【解决方案1】:

    您始终可以将原始 vega-lite dict 传递给 Altair 中的任何属性:

    source = data.stocks()
    
    alt.Chart(source).mark_line(
        point={
          "filled": False,
          "fill": "white"
        }
    ).encode(
        x='date',
        y='price',
        color='symbol'
    )
    

    或者您可以检查 mark_line() 的文档字符串并查看它期望指向 OverlayMarkDef() 并使用 Python 包装器:

    alt.Chart(source).mark_line(
        point=alt.OverlayMarkDef(filled=False, fill='white')
    ).encode(
        x='date',
        y='price',
        color='symbol'
    )
    

    【讨论】:

    • 我有一个后续问题。如果我在文档中搜索“mark_line”,我发现所有参数都设置为“未定义”。这是我的意思的链接。 altair-viz.github.io/user_guide/generated/toplevel/… 。应该如何为参数找到合适的类型?
    • 我想通了,mark_line > MarkDef > OverlayMarkDef。我必须深入挖掘!
    • 它有一个指向MarkDef的链接,其中包含有关参数的更多详细信息。
    【解决方案2】:

    您可以将更多信息传递给 point 参数,类似于指定 vega-lite 的方式。

    import altair as alt
    from vega_datasets import data
    
    source = data.stocks()
    
    alt.Chart(source).mark_line(
        point={
          "filled": False,
          "fill": "white"
        }
    ).encode(
        x='date',
        y='price',
        color='symbol'
    )
    

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多