Search code examples
pythonchartssymbolspointaltair

Altair: change point symbol



I use the code below to create this faceted chart:

https://vega.github.io/editor/#/gist/a9f238f389418c106b7aacaa10561281/spec.json

I would like to use another symbol (a dash in example) instead of the gray points.

I found a lot of examples using a field to render a point by value, but I haven't found how to apply a specific symbol to all points of a layer.

Thank you

df=pd.read_csv("tmp_reshape.csv",keep_default_na=False)

mean=alt.Chart(df).mark_line(color="#0000ff",strokeWidth=1).encode(
    alt.X('period:O'),
    alt.Y('mean:Q',title=None, scale=alt.Scale(zero=False))
)

median=alt.Chart(df).mark_line(color="#ffa500",strokeWidth=1,strokeDash=[5, 5]).encode(
    alt.X('period:O'),
    alt.Y('median:Q',title=None, scale=alt.Scale(zero=False))
)

minmax=alt.Chart(df).mark_rule(color="grey",strokeWidth=0.5).encode(
    alt.X('period:O'),
    alt.Y('minimum:Q',title=None),
    alt.Y2('maximum:Q',title=None)
)

min=alt.Chart(df).mark_point(filled=True,color="grey",size=15).encode(
    alt.X('period:O'),
    alt.Y('minimum:Q',title=None)
)

max=alt.Chart(df).mark_point(filled=True,color="grey",size=15).encode(
    alt.X('period:O'),
    alt.Y('maximum:Q',title=None)
)

alt.layer(minmax,min,max,median,mean).properties(width=470,height=100).facet(row='param:N').resolve_scale(y='independent')

Solution

  • Use shape="stroke" for a single chart as such:

    import altair as alt
    from vega_datasets import data
    
    source = data.cars()
    
    alt.Chart(source).mark_point(
        shape="stroke"
    ).encode(x="Horsepower:Q", y="Miles_per_Gallon:Q")
    

    enter image description here

    Or use a global .configure_point() for a layered chart:

    import altair as alt
    from vega_datasets import data
    
    source = data.cars()
    
    c = alt.Chart(source).mark_point().encode(
        x="Horsepower:Q", 
        y="Miles_per_Gallon:Q"
    )
    
    alt.layer(c, c).configure_point(
        shape="stroke"
    )
    
    

    More info here: https://altair-viz.github.io/altair-tutorial/notebooks/08-Configuration.html#top-level-configuration