Search code examples
pythonaltair

Is there a way to change the default resolution for downloading .png images from jupyter notebooks in Altair?


Is there a way to change the default Altair settings such that, when I click (...) in the upper right-hand corner of a figure in a jupyter notebook and then select "Save as PNG", it saves the figure with a resolution/dpi that I manually set?


Solution

  • Yes, you can use chart.display(scaleFactor=num):

    import altair as alt
    import pandas as pd
    
    
    source = pd.DataFrame({
        'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
    })
    
    alt.Chart(source).mark_point().encode(
        x='b',
        y='b'
    ).display(
        scaleFactor=2
    )
    

    The default resolution when you are saving a chart in Altair, is the width and height specified for the chart plus some padding for the axes labels etc. For a chart with two continuous axes, such as a scatter plot, the default in Altair is a height of 300 px and a width of 400 px (for the area where the points reside). Together with the padding for the axis labels, the final size of the downloaded chart is 450 x 347 in my testing. The scaleFactor is used to multiply the number of pixels used for the width and height, so setting it to 2 would use twice as many pixels along each dimension so that the image image size becomes 900 x 694 px.

    Ref Changing the "save as" filename for Altair chart in google colab for more options