Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altair: Line Chart with Stroked Point Markers

I'm trying to create a line chart with point markers in Altair. I'm using the multi-series line chart example from Altair's documentation and trying to combine it with the line chart with stroked point markers example from Vega-Lite's documentation.

Where I'm confused is how to handle the 'mark_line' argument. From the Vega example, I need use "point" and then set "filled" to False.

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

How would I apply that in Altair? I figured out that setting 'point' to 'True' or '{}' added a point marker, but confused on how to get the fill to work.

source = data.stocks()

alt.Chart(source).mark_line(
    point=True
).encode(
    x='date',
    y='price',
    color='symbol'
)
like image 921
Ragnar Lothbrok Avatar asked Jan 25 '23 05:01

Ragnar Lothbrok


2 Answers

You can always pass a raw vega-lite dict to any property in Altair:

source = data.stocks()

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

or you can check the docstring of mark_line() and see that it expects point to be an OverlayMarkDef() and use the Python wrappers:

alt.Chart(source).mark_line(
    point=alt.OverlayMarkDef(filled=False, fill='white')
).encode(
    x='date',
    y='price',
    color='symbol'
)
like image 111
jakevdp Avatar answered Jan 27 '23 20:01

jakevdp


You can pass further information to the point parameter similar to how the vega-lite is specified.

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

enter image description here

like image 27
eitanlees Avatar answered Jan 27 '23 18:01

eitanlees