1

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?

joelostblom
  • 43,590
  • 17
  • 150
  • 159
mdeverna
  • 322
  • 3
  • 9

1 Answers1

1

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

joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • Oh man, @joelostblom, you're the greatest. Seriously, I always end up reading your `altair` answers — very very appreciated. One more question: What is the scaleFactor doing / how does setting higher (or lower) values change the image? Thanks. – mdeverna Jul 21 '22 at 23:09
  • 1
    =) Thank you for the kind words @mdeverna, I am happy you're finding my answers helpful! I updated this one to address your question. – joelostblom Jul 22 '22 at 09:21