python type_error ai_generated true

TypeError: 'bins'必须是整数、标量序列或字符串,而不是字符串列表。

TypeError: 'bins' must be an integer, a sequence of scalars, or a string, not <class 'list'> of strings.

ID: python/matplotlib-histogram-bins-error

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

向plt.hist()传递了字符串列表作为bins参数,但该参数需要数值型bins或单个字符串方法(如'auto'、'fd')。

English

Passing a list of strings as bins to plt.hist(), which expects numeric bins or a single string method (like 'auto', 'fd').

generic

解决方案

  1. 95% 成功率 Use an integer for the number of bins
    plt.hist(data, bins=20)
  2. 90% 成功率 Use a sequence of bin edges as numbers
    plt.hist(data, bins=[0, 10, 20, 30])

无效尝试

常见但无效的做法:

  1. Using a list of integers but with mixed types 80% 失败

    Bins must be numeric; mixed types cause conversion errors.

  2. Passing a single string like 'auto' but in a list 90% 失败

    A list of one string is still a list, not a valid bins argument.