python data_error ai_generated true

UserWarning: 数据没有正值,因此无法进行对数缩放。

UserWarning: Data has no positive values, and therefore cannot be log-scaled.

ID: python/matplotlib-log-scale-nonpositive

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-11-03首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

在仅包含零或负值的数据轴上设置对数缩放。

English

Setting a log scale on an axis whose data contains only zero or negative values.

generic

解决方案

  1. 90% 成功率
    ax.set_yscale('symlog', linthresh=1)
  2. 88% 成功率
    import numpy as np
    mask = data > 0
    ax.plot(x[mask], data[mask])
    ax.set_yscale('log')

无效尝试

常见但无效的做法:

  1. 55% 失败

    Clipping silently replaces values with the smallest positive number, producing misleading plots.

  2. 50% 失败

    Shifts the entire distribution and can hide genuinely zero categories.