首页 新闻 会员 周边 捐助

使用altair保存图片时报错

0
[已解决问题] 解决于 2024-01-17 11:57

运行在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

如何正确保存?

October-的主页 October- | 初学一级 | 园豆:164
提问于:2024-01-17 11:49
< >
分享
最佳答案
0

根据报错信息疑似是版本问题,但此时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
October- | 初学一级 |园豆:164 | 2024-01-17 11:56

PyCharm 2023.3.2 (Professional Edition) 版本下打开的 .ipynb 展示 altair 结果图时可能无法显示,建议使用浏览器打开。

October- | 园豆:164 (初学一级) | 2024-01-20 19:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册