在 Jupyter Notebook 中更改 Altair 绘图的大小

dre*_*ww2 9 python altair

我在 Jupyter 笔记本(不是 JupyterLab)中使用以下方法渲染 Altair 图:

alt.renderers.enable('notebook')
Run Code Online (Sandbox Code Playgroud)

一切正常,但是相对于我的 Jupyter 笔记本的宽度,这些图通常很小。

如果我使用以下方法将笔记本的宽度扩展到屏幕的 100%:

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

Altair 图不会相应地缩放(它们保持相同的大小)。

有什么方法可以缩放渲染图的大小(即,使它们更大),同时仍将它们保留在笔记本中?

谢谢!

jak*_*vdp 20

无法将 Altair 图表自动缩放到浏览器窗口的大小。图表的大小由视图配置和/或图表规范中的widthheight属性控制;例如:

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
)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明