[Python] When Altair saves plot with save, it loses some configuration parameters

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I made a heatmap with Altair with wide borders between the cells, in Jupyter notebook. When I print it in notebook it looks like it is supposed to - with wide borders, However when I save it as png, svg or pdf, there is no borders at all.

    In some other cases there are some borders in png and svg, but super thin, but in pdf they are always fully absent.

    I am using Altair version 5.3.0 with Python 3.8

    How to make pictures from the save() function be identical to what is shown in Jupyter notebook?

    import altair as alt
    import pandas as pd

    # create a sample dataset
    data_df = pd.DataFrame({'X': [1,2,3,4,5,6,7,8,9,10], 'Y': [1,2,3,4,5,6,7,8,9,10], 'Intensity': [0,0,0,0,0,1,1,1,1,1]})
    intensity_vars = [0,1]
    range_colors = ['#208943', '#c0e6ba']

    # set up the heatmap
    heatmap = alt.Chart(data_df).mark_rect(stroke='w', strokeWidth=5).encode(
    x = alt.X('X:N'),
    y = alt.Y("Y:N"),
    color = alt.Color("Intensity:N", scale = alt.Scale(domain=intensity_vars, range = range_colors))
    )

    # Draw the chart
    heatmap.save("test.png")
    heatmap.save("test.svg")
    heatmap.save("test.pdf")

    heatmap


    Plot from the notebook:
    [​IMG]

    png plot (svg and pdf are identical):
    [​IMG]

    Continue reading...

Compartilhe esta Página