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

如何优化Plotly图表空值检测函数并获取测试方案与反例

检测Plotly图表是否为空的通用函数优化方案

需求背景

从API获取Plotly图表数据,无法访问原始输入数据框,需实现自动化测试,无需可视化即可判断图表是否为空。

现有初步实现

def check_plot_data_empty(plot_data):
    if 'data' in plot_data:
        for trace in plot_data['data']:
            data_keys = ['x', 'y', 'z', 'values', 'text', 'marker']
            for key in data_keys:
                if key in trace:
                    if key == 'marker':
                        if 'size' in trace['marker'] and len(trace['marker']['size']) > 0:
                            return False
                        if 'color' in trace['marker'] and len(trace['marker']['color']) > 0:
                            return False
                    else:
                        if isinstance(trace[key], list) and len(trace[key]) > 0:
                            return False
                        if key == 'text' and isinstance(trace[key], str) and trace[key].strip():
                            return False
    return True

现有函数的失效反例

以下场景中,现有函数会做出错误判断:

1. 包含未覆盖文本属性的非空图表

当trace包含hovertext(或customdata)等未加入检查列表的字段且有非空内容时,现有函数会误判为空图表:

plot_data = {
    'data': [
        {'type': 'scatter', 'x': [], 'y': [], 'hovertext': 'Hoverable text content'}
    ]
}

现有函数返回True(认为空),但实际图表hover时会显示文本,不属于空图表。

2. Pie图带有非空labels但空values

Pie图的核心字段labels未被现有函数检查,若labels非空但values为空,函数会误判为空:

plot_data = {
    'data': [
        {'type': 'pie', 'labels': ['Category A', 'Category B'], 'values': []}
    ]
}

现有函数返回True,但实际图表会显示标签,不属于空图表。

3. Text字段为非空列表时误判

现有函数仅处理字符串类型的text,若text为非空列表(如Pie图的文本标签),函数无法识别:

plot_data = {
    'data': [
        {'type': 'pie', 'values': [], 'text': ['Label 1', 'Label 2']}
    ]
}

现有函数返回True,但实际存在非空文本内容,图表并非空。

4. Marker包含其他非空属性

现有函数仅检查marker.size和marker.color,若marker有其他非空属性(如symbol列表),函数会误判:

plot_data = {
    'data': [
        {'type': 'scatter', 'x': [], 'y': [], 'marker': {'symbol': ['circle', 'square']}}
    ]
}

现有函数返回True,但实际marker存在有效样式配置,图表并非空。

5. Heatmap带有非空二维文本矩阵

Heatmap的text通常为二维列表,现有函数无法识别这种非空情况:

plot_data = {
    'data': [
        {'type': 'heatmap', 'z': [], 'text': [['Cell 1', 'Cell 2'], ['Cell 3', 'Cell 4']]}
    ]
}

现有函数返回True,但实际存在非空文本内容,图表并非空。

更健壮的测试方案

1. 扩展字段覆盖范围

补充Plotly各trace类型的核心数据字段,比如:

  • 文本类:hovertext、customdata、texttemplate
  • 类型专属字段:Pie图的labels、Heatmap的z(二维数组)
  • Marker扩展属性:symbol、opacity

2. 利用Plotly内置结构判断

借助plotly.graph_objects.Figure加载图表数据,针对不同trace类型做针对性检查:

from plotly.graph_objects import Figure

def check_plot_empty(plot_data):
    fig = Figure(plot_data)
    for trace in fig.data:
        # 散点图检查
        if trace.type == 'scatter':
            if len(trace.x) > 0 or len(trace.y) > 0:
                return False
            # 检查文本类字段
            for text_field in ['hovertext', 'text', 'customdata']:
                if hasattr(trace, text_field):
                    val = getattr(trace, text_field)
                    if isinstance(val, list) and len(val) > 0:
                        return False
                    if isinstance(val, str) and val.strip():
                        return False
        # 饼图检查
        elif trace.type == 'pie':
            if len(trace.values) > 0 or len(trace.labels) > 0:
                return False
            if hasattr(trace, 'text') and isinstance(trace.text, list) and len(trace.text) > 0:
                return False
        # 热力图检查
        elif trace.type == 'heatmap':
            if len(trace.z) > 0 or (trace.text and any(len(row) > 0 for row in trace.text)):
                return False
        # 其他trace类型(如bar、box等)可按需补充
    return True

3. 完善自动化测试用例

编写单元测试覆盖以下场景:

  • 纯空图表(所有核心字段为空)
  • 带非空文本属性的"空"数据图表
  • 不同trace类型的空/非边界情况
  • 包含非数据配置(如trace name)的空图表

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:19:55