Plotly循环绘制图表时报Orca服务通信错误如何解决
问题根因
该报错是循环中快速连续调用fig.show()时,orca服务来不及处理并发的渲染请求,连接队列被占满导致通信中断,属于旧版本Plotly依赖orca做静态渲染的典型并发问题。
解决方法
- 方法1:添加渲染间隔,每次调用
fig.show()后插入短暂休眠,给orca留出处理时间
代码修改如下:import plotly.figure_factory as ff import time for columns in data.columns[1:-1]: x1 = data.loc[df.TARGET==0, columns] x2 = data.loc[df.TARGET==1, columns] fig = ff.create_distplot([x1,x2], group_labels, show_rug=False) fig.update(layout_title_text=columns) fig.show() time.sleep(2) # 可根据实际渲染速度调整休眠时长 - 方法2:更换渲染后端,放弃orca改用浏览器渲染,无需依赖本地orca服务
运行代码前添加全局配置:import plotly.io as pio pio.renderers.default = 'browser' - 方法3:升级Plotly到4.0及以上版本,新版本默认使用kaleido作为静态渲染后端,完全替代orca,不存在此类连接冲突问题
升级命令:pip install --upgrade plotly - 方法4:如果需要批量保存图片而非实时预览,建议直接把图表写入本地文件,不要循环调用
fig.show(),参考写法:for columns in data.columns[1:-1]: x1 = data.loc[df.TARGET==0, columns] x2 = data.loc[df.TARGET==1, columns] fig = ff.create_distplot([x1,x2], group_labels, show_rug=False) fig.update(layout_title_text=columns) fig.write_html(f"{columns}_dist.html") # 保存为可交互html文件 # 也可保存为静态图片 fig.write_image(f"{columns}_dist.png")
内容的提问来源于stack exchange,提问作者RMteam
相关产品推荐
相关产品推荐

