运行在ipynb中
def example_mask():
LS_data = pd.concat(
[
pd.DataFrame(
{
"Subsequent Mask": subsequent_mask(20)[0][x, y].flatten(),
"Window": y,
"Masking": x,
}
)
for y in range(20)
for x in range(20)
]
)
return (
alt.Chart(LS_data)
.mark_rect()
.properties(height=250, width=250)
.encode(
alt.X("Window:O"),
alt.Y("Masking:O"),
alt.Color("Subsequent Mask:Q", scale=alt.Scale(scheme="viridis")),
)
.interactive()
)
example_mask().save("subsequent_mask.svg")
但是发生报错:
NoMatchingVersions: No matches for version='5.16.3' among ['4.0.2', '4.8.1', '4.17.0'].
Often this can be fixed by updating altair_viewer:
pip install -U altair_viewer
相关版本:
altair 5.0.1 py311haa95532_0
altair-data-server 0.4.1 pypi_0 pypi
altair-saver 0.5.0 pypi_0 pypi
altair-viewer 0.4.0 pypi_0 pypi
如何正确保存?
根据报错信息疑似是版本问题,但此时altair-viewer
已是最新,所以考虑其他情况。
先更新下altair:
pip install -U altair
此时再运行报错信息为
AltairDeprecationWarning: The altair_saver export engine is deprecated and will be removed in a future version.
Please migrate to the vl-convert engine
warnings.warn(
NoMatchingVersions: No matches for version='5.16.3' among ['4.0.2', '4.8.1', '4.17.0'].
Often this can be fixed by updating altair_viewer:
pip install -U altair_viewer
猜测是altair-saver
不再被支持而引发的错误,因此卸载altair-saver
安装vl-convert
:
pip uninstall altair_saver
pip install vl-convert-python
此时问题解决!
附上最后的相关版本
# Name Version
altair 5.2.0
altair-data-server 0.4.1
altair-saver 0.5.0
altair-viewer 0.4.0
vl-convert-python 1.2.0
PyCharm 2023.3.2 (Professional Edition) 版本下打开的 .ipynb 展示 altair 结果图时可能无法显示,建议使用浏览器打开。