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

Plotly Python在Azure Functions中导出图片仅显示灰色区域的异常问题

Plotly+Kaleido在Azure Functions中导出图表仅显示填充区域,折线缺失的问题解决思路

问题场景

本地使用Python Plotly(5.11.0)和Kaleido(0.1.*)导出PNG格式图表时,包含两条折线及一条灰色填充面积图的图表显示正常。但在Azure Functions环境执行相同代码时,仅灰色填充区域被导出,其余折线完全缺失。

核心代码如下:

fig = px.line(df_unpivot[df_unpivot['column name']!='var_2'], x = 'index_series', y ='yName', color= 'Data type', labels={
                     "index_series": "Period",
            
                 })
fig.add_trace(go.Scatter(x=df['index_series'], y=df['var_2'], line_color='grey', mode = 'lines', name= 'Variable name', fill='tozeroy'))

logging.info (fig)

logging.info('dataframe head - {}'.format(df.head()))
logging.info('dataframe head - {}'.format(df_unpivot.head()))
logging.info ('Created the chart, exporting to sharepoint')
#fig.show()
local_path = tempfile.gettempdir()
local_dir = os.path.abspath(local_path)
path = os.path.join(local_dir, 'imagem.png')
fig.write_image(path, validate = True)
with open(path, 'rb') as content_file:
    file_content = content_file.read()

已完成的排查

  • 对比本地与Azure环境中的fig对象,结构完全一致,排除图表生成环节的问题
  • 尝试过px.area等其他绘图方式,以及fig.to_image导出方法,问题复现
  • 修改导出格式、缩放比例等参数,无效
  • 减少数据量(通过df.iloc截取部分数据)时,行数低于某阈值可正常导出所有线条,超过阈值后异常;即使移除填充效果,问题仍然存在,推测与Kaleido导出时的内存限制相关

可行的解决方向

1. 调整Kaleido内存配置

  • 在Azure Functions配置中提升函数实例的内存配额(比如从默认1GB调整至2GB)
  • 初始化Kaleido时手动设置内存上限,示例代码:
    from kaleido.scopes.plotly import PlotlyScope
    scope = PlotlyScope(
        max_memory=2*1024*1024*1024  # 设置为2GB内存限制
    )
    img_bytes = scope.transform(fig, format="png")
    

2. 优化图表数据

  • 对大数据集做降采样处理,比如用pandas.resample按时间周期聚合,减少绘图点数量
  • 移除冗余的图表元素(如过多的hover细节、不必要的布局样式),降低渲染负载

3. 升级依赖版本

  • 将Kaleido升级到0.2.x版本(兼容Plotly 5.x),新版本可能修复了内存管理相关的bug
  • 确保Plotly与Kaleido版本匹配,避免版本兼容冲突

4. 调整Azure Functions运行环境

  • 切换到Premium或Dedicated计划,这类计划提供更高的CPU和内存资源,避开消费计划的资源限制
  • 在应用设置中配置WEBSITE_MEMORY_LIMIT_MB,明确指定函数的内存配额

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:47:04