You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly图表存为PNG并打包Zip速度过慢,求优化方案

Plotly图表+文本打包Zip的性能优化问题

问题描述

需要将Plotly生成的图表保存为PNG图片,同时把程序生成的文本内容保存为TXT文件,将两者一同打包到Zip压缩包中。现有代码能生成目标Zip文件,但运行耗时约5秒,希望确认实现是否存在问题,并获取更优实现方式。

原实现代码

import io
import zipfile
import plotly.express as px
import kaleido

#write texts to stringIO
s = io.StringIO()
texts = ['foo','bar']
newline = '\n'
for text in texts:
    s.write(text)
    s.write(newline)

#create sample plotly figure
long_df = px.data.medals_long() 
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")

zip_buffer = io.BytesIO()    
with zipfile.ZipFile(zip_buffer, mode="w") as zf:
        fig_png = fig.to_image(format="png") # kaleido library
        buf = io.BytesIO(fig_png)
        zf.writestr('barplot.png', buf.getvalue()) #write plot as png file
        zf.writestr('text.txt', s.getvalue())   #write texts as text file
        
with open("a.zip", "wb") as f:
    f.write(zip_buffer.getbuffer())

耗时原因分析

你的代码逻辑本身没有错误,耗时的核心环节是**fig.to_image()调用Kaleido渲染PNG**——Kaleido依赖Chromium内核渲染图表,启动内核的过程本身就需要一定时间,这是这类工具的固有开销,首次运行时该表现尤为明显。

优化实现方案

1. 复用Kaleido渲染实例(批量场景收益显著)

如果需要多次渲染图表,复用PlotlyScope实例可以避免重复启动Chromium内核,大幅降低累计耗时;单次渲染时也能小幅减少初始化开销:

import io
import zipfile
import plotly.express as px
from kaleido.scopes.plotly import PlotlyScope

# 预创建Scope实例,复用内核
scope = PlotlyScope()

# 简化文本生成逻辑
text_content = '\n'.join(['foo', 'bar'])

# 创建图表
long_df = px.data.medals_long() 
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")

zip_buffer = io.BytesIO()    
with zipfile.ZipFile(zip_buffer, mode="w") as zf:
    # 用预创建的scope渲染图表
    fig_png = scope.transform(fig, format="png")
    zf.writestr('barplot.png', fig_png)
    zf.writestr('text.txt', text_content)
        
with open("a.zip", "wb") as f:
    f.write(zip_buffer.getbuffer())

2. 简化文本生成逻辑

原代码的文本写入可以直接用'\n'.join()完成,减少StringIO的IO操作次数,虽然这部分耗时占比极低,但能让代码更简洁高效。

3. 替换轻量渲染后端(可选)

如果对图表渲染效果要求不高,可以考虑用Matplotlib替代Plotly生成简单图表,渲染速度会大幅提升;或者尝试使用已停止维护但启动更快的orca后端(官方推荐优先使用Kaleido)。

总结

单次运行时5秒左右的耗时属于Kaleido内核启动的正常现象。如果是批量处理图表,复用Kaleido的Scope实例能显著降低累计耗时;日常使用中,上述优化后的代码在逻辑简洁性和性能上都更优。

内容的提问来源于stack exchange,提问作者user173729

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 03:22:49