Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the size of Altair plot renders in Jupyter notebook

Tags:

python

altair

I am rendering Altair plots in Jupyter notebook (not JupyterLab) using:

alt.renderers.enable('notebook')

And everything works fine, however the plots are often small relative to the width of my Jupyter notebook.

If I expand the width of the notebook to 100% of my screen using:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

Altair plots don't scale accordingly (they stay the same size).

Is there any way to scale the size of rendered plots (i.e., make them bigger) while still keeping them within the notebook?

Thanks!

like image 643
dreww2 Avatar asked Aug 28 '19 14:08

dreww2


People also ask

How do I display Altair chart in Jupyter notebook?

If the results are different, you can use Kernel -> Change Kernel in the Jupyter notebook menu to change to a kernel with a working Altair version. If that's not the case, then it is likely that you have inadvertently enabled a different renderer in one of the notebooks, for example by running alt. renderers.

How do you save Altair's plot?

The fundamental chart representation output by Altair is a JSON string format; you can save a chart to a JSON file using Chart. save() , by passing a filename with a . json extension.

What is Python Altair?

Altair is a declarative statistical visualization library for Python, based on Vega and Vega-Lite. Altair offers a powerful and concise visualization grammar that enables you to build a wide range of statistical visualizations quickly.


1 Answers

There is no way to automatically scale Altair charts to the size of the browser window. The size of charts is controlled by the view configuration and/or the width and height properties in the chart specification; for example:

import altair as alt
from vega_datasets import data
cars = data.cars()

alt.Chart(cars).mark_point().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin'
).properties(
    width=800,
    height=300
)

enter image description here

like image 166
jakevdp Avatar answered Sep 21 '22 05:09

jakevdp