Just logging this here as I had the same issue and found a solution:
As per the Vega documentation, you can set width
to container
, which will adjust the chart size to its surrounding HTML container.
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='container'
)
N.b.: This does not work with the standard alt.Chart.save
method, as the built-in HTML template does not anticipate responsive styling.
Here's the relevant snippets of how I got it to run (in a standard Flask/Jinja2 stack - you can adapt this to run in a Jupyter notebook using IPython.display.display
and IPython.display.HTML
):
app.py
Serve the chart by rendering an HTML template, containing the chart
as a JSON object which represents the Vega spec (Vega is the underlying JavaScript library that renders Altair charts).
@app.route('/chart')
def chart():
the_chart = alt.Chart(...).properties(width='container')
return render_template('chart.html.jinja2', chart=the_chart.to_json())
chart.html.jinja2
Template to embed a chart
...
<!-- anchor point: this is an empty `div` that will be filled with the chart -->
<div id="chart_pl" class="altair"></div>
...
<!-- import vega libraries -->
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<!-- render the chart with vega -->
<script type="text/javascript">
<!-- the JSON object needs to mark as `safe` so it doesn't get HTML-quoted -->
var spec = {{ chart|safe }};
<!-- some vega options -->
var opt = {"renderer": "canvas", "actions": false};
<!-- embed the chart into div with ID "chart" -->
vegaEmbed("#chart_pl", spec, opt);
</script>
style.css
Ensure that the div
we marked as class="altair"
gets sized appropriately
.altair {
width: 100%;
}