【问题标题】:ValueError in matplotlib even though arrarys are the same length即使数组长度相同,matplotlib 中的 ValueError
【发布时间】:2019-03-18 03:11:34
【问题描述】:

我的数组如下:

x = ['2.000000', '2.100000', '2.200000', '2.300000', '2.400000', '2.500000']
y = ['-0.876484', '-0.841230', '-0.776523', '-0.724883', '-0.656426', '-0.595879']
e = ['0.000655', '0.000851', '0.001311', '0.001642', '0.001702', '0.001709']

我用误差线绘制的代码如下:

import matplotlib.pyplot as plt

x = ['2.000000', '2.100000', '2.200000', '2.300000', '2.400000', '2.500000']
y = ['-0.876484', '-0.841230', '-0.776523', '-0.724883', '-0.656426', '-0.595879']
e = ['0.000655', '0.000851', '0.001311', '0.001642', '0.001702', '0.001709']
plt.errorbar(x, y, yerr=e, fmt='o')
plt.show()

我不断收到 ValueError 说:

ValueError: err must be a scalar, the same dimensions as x, or 2xN.

我不明白为什么会这样,因为我所有数组的维度都是相等的。

【问题讨论】:

  • 我认为你的数据都是字符串是个问题。试试plt.errorbar(list(map(float, x)), list(map(float, y)), yerr=list(map(float, e)), fmt='o')

标签: python matplotlib valueerror


【解决方案1】:

您正在绘制字符串,这就是 matplotlib 无法处理 e 的形状的原因。 将您的数据更改为数字并且它可以工作:(您的e 太小,这就是错误栏不显示的原因)

x = [2.000000, 2.100000, 2.200000, 2.300000, 2.400000, 2.500000]
y = [-0.876484, -0.841230, -0.776523, -0.724883, -0.656426, 0.595879]
e = [0.000655, 0.000851, 0.001311, 0.001642, 0.001702, 0.001709]

plt.errorbar(x, y, yerr=e, fmt='o')
plt.show()

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2021-12-07
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多