Search code examples
pythonpandasaltair

How to change background of Altair plot in VSCode


I have a plot created using pandas and altair using the following code in VSCode

alt.Chart(df).mark_point().encode(
    alt.X("fertility"),
    alt.Y("cluster")
)

How do I change the background colour? I'd rather like the background white or off-white.

enter image description here


Solution

  • You can chain it with configure_view :

    np.random.seed(123456)
    
    df = pd.DataFrame({
        "fertility": np.random.uniform(0, 9, 100), "cluster": np.random.randint(0, 6, 100)}
    )
    
    chart = alt.Chart(df).mark_point().encode(
        alt.X("fertility"), alt.Y("cluster")).configure_view(fill="white")
    
    #pip install altair_viewer
    chart.show() #or chart (in VSCode)
    

    Output :

    enter image description here