7

Can one make Altair plots fit the screen size, rather than have a pixel-defined width and height? I've read things about autosize "fit", but I am unsure about where to specify these.

3 Answers 3

6

There is no way to do this. The dimensions of Altair/Vega-Lite charts are pre-determined by the chart specification and data, and cannot be made to scale with the size of the browser window.

6

While it is true that the vega-lite determines the size of the chart itself, it is possible to treat the chart (rendered as canvas) like an image.

I used an example from w3css which applied to my vega-light charts scales the charts accordingly. Basically it is then scaled proportionally according to the surrounding container.

Example HTML generated by vega-light:

<div id="visInteractiveYear" class="vega-embed">
<canvas width="1366" height="960" class="marks" style="width: 976px; height: 686px;"></canvas>
</div>

Here the CSS snippet, that basically overwrite width/height (style) of the canvas:

canvas.marks {
    max-width: 100%!important;
    height: auto!important;
}

Here a test without any scaling and its original size:

Normal Chart (no scaling)

Here a test with scaling to fit within the surrounding box.

Chart scaled down via CSS

If you are using interactive charts, the scaling might be a problem. I guess that the click positions are not correctly translated (since canvas is scaled, but the logic of vega does not know about it), thus strange behaviour is the result. In my case, the selection has always an offset to the mouse cursor.

Also, the user has to zoom in to be able to read the chart, since it is proportionally scaled. Probably in most cases not the ideal user-friendly way to go.

Maybe a better option is to switch to another vega-light spec, which renders the chart for the specific display size, or to switch to a different representation (e.g., a standard list), which can then easily be read on smaller displays (adaptive design).

For complex charts, opening the chart in another browser tab, might be also be a good solution. The user knows about the new tab, the mobile browser has to show only one chart/image, so there is not much clutter to worry about. Thus, it is easy to navigate/scroll in the new tab, since it only contains the chart and maybe a back/close tab button.

2
  • Thanks for the warning about interactive charts. Perhaps there is a way to turn the interactivity off when the chart is scaled?
    – Geoff D
    Apr 27, 2022 at 21:33
  • @GeoffD i think the best option is to switch to another vega-light spec when the layout changes and interactive chart elements are required.
    – holsteto
    Jan 24 at 9:49
3

If you are using Altair 4.0 or greater you can use properties() and container:

import altair as alt
from vega_datasets import data

source = data.cars()

plot = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).properties(
    width='container',
    height='container'
)

This will make it responsive, note that this is the size of each plot if you have multiple it doesn't work as you would expect but instead each plot will have the size of the parent container.

5
  • 1
    I'd like to make this the accepted answer, but I haven't tested it myself to be sure.
    – herman
    Jan 4, 2020 at 20:40
  • I suppose there's no way to make it work for multiple plots yet..
    – bio
    Dec 19, 2020 at 8:43
  • 6
    This does not work me unfortunately in Jupyter Notebook with Altair 4.1.0. As soon as I specify width='container' and height='container' the graph collapses to an invisible dot. Jan 19, 2021 at 18:31
  • @AntonGolubev mine does the same. Have you figured out a way to deal with that? Jul 30, 2021 at 22:17
  • @UalasRohrer unfortunately not. Aug 3, 2021 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.