Search code examples
altair

Extracting legend of the plot in Altair


I am trying to extract the legend of the plot in Altair. The problem is that I have trouble finding a way to remove the square and the circle inside the square next to the legend. Here is a sample code and the result I have. Any suggestion and help is appreciated.

import pandas as pd
import altair as alt

df = pd.DataFrame({'x':['a','b','c'],
                    'y':[1,2,3]})

legend = alt.Chart(df).mark_point().encode(
    color=alt.Color('x:N')
)
legend

enter image description here


Solution

  • You can't "extract" the legend, but you could make the rest of the plot invisible (there will still be some whitspace to the left):

    alt.Chart(df).mark_point(size=0).encode(
        color=alt.Color('x:N')
    ).configure_view(strokeWidth=0)
    

    enter image description here