Windows11下Plotly write_image函数无响应问题求助
问题描述
在Ubuntu 22.04系统中运行以下代码可正常生成图片,但在Windows 11(Python 3.12、Plotly 5.20.0)环境下执行时,程序卡在fig.write_image()代码行,仅打印“save start”后便无限挂起,甚至无法通过Ctrl+C中断。
#!/usr/bin/env python3 #%% import plotly.graph_objects as go fig = go.Figure( data=[go.Scatter(x=[0, 1], y=[0, 1])], layout=go.Layout( xaxis=dict(range=[0, 5], autorange=False), yaxis=dict(range=[0, 5], autorange=False), title="Start Title", updatemenus=[dict( type="buttons", buttons=[dict(label="Play", method="animate", args=[None])])] ), frames=[go.Frame(data=[go.Scatter(x=[1, 2], y=[1, 2])]), go.Frame(data=[go.Scatter(x=[1, 4], y=[1, 4])]), go.Frame(data=[go.Scatter(x=[3, 4], y=[3, 4])], layout=go.Layout(title_text="End Title"))] ) pngname = "demo.png" print("save start") fig.write_image(pngname,format="png",width=800,height=500) print("save image done")
解决方案
- 降级Python版本:Python 3.12与Plotly 5.20.0在Windows环境存在兼容性问题,暂时降级到Python 3.11后重新安装Plotly,多数情况下可解决挂起问题。
- 指定渲染引擎:在
fig.write_image()中显式指定渲染引擎为kaleido,需先安装依赖:
修改代码:pip install kaleidofig.write_image(pngname, format="png", width=800, height=500, engine="kaleido") - 升级Plotly版本:将Plotly更新至最新稳定版,新版本可能修复了Python 3.12的适配问题:
pip install --upgrade plotly - 排查动画组件:若无需动画功能,临时移除代码中的
updatemenus和frames部分,测试基础图表能否正常保存,以此确认是否为动画组件触发的挂起问题。
内容的提问来源于stack exchange,提问作者beetlej
相关产品推荐
相关产品推荐

