简体   繁体   English

ValueError:x 和 y 必须具有相同的第一维,但具有形状 (512,) 和 (256,)

[英]ValueError: x and y must have same first dimension, but have shapes (512,) and (256,)

I tried to use numpy and np.array but it fails.我尝试使用 numpy 和 np.array 但失败了。 It displays that error: ValueError: x and y must have same first dimension, but have shapes (512,) and (256,).它显示该错误:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (512,) 和 (256,)。 I dont know how to edit that code.我不知道如何编辑该代码。 Please help.请帮忙。

import math as mt
import matplotlib.pylab as plt

N = 512
t_min = 0   # [s]
t_max = 2   # [s]
frq = 2     # [Hz]
T_p = (t_max-t_min)/N
lst = range(0, N, 1)

t = []
sinus = []
rect = []
pila1 = []
pila2 = []

for i in lst:
    t.extend([t_min + lst[i] * T_p])
    sinus.extend([mt.sin(2 * mt.pi * frq * t[i])])
    if sinus[i] > 0:
        rect.extend([True])
    else:
        rect.extend([False])
        pila1.extend([(t[i] % (1 / frq)) * frq])
        pila2.extend([abs((t[i] % (1 / frq)) * frq - 0.5) * 2])


plt.figure(1)
plt.title('Plot')
plt.plot(t, sinus)
plt.plot(t, rect)
plt.plot(t, pila1)
plt.plot(t, pila2)
plt.xlabel('t')
plt.grid(True)


plt.show()

there is full error:有完整的错误:

    Traceback (most recent call last):
  File "venv/zadanieA.py", line 32, in <module>
    plt.plot(t, pila1)
  File "venv\lib\site-packages\matplotlib\pyplot.py", line 2761, in plot
    return gca().plot(
  File "venv\lib\site-packages\matplotlib\axes\_axes.py", line 1646, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "venv\lib\site-packages\matplotlib\axes\_base.py", line 216, in __call__
    yield from self._plot_args(this, kwargs)
  File "venv\lib\site-packages\matplotlib\axes\_base.py", line 342, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (512,) and (256,)

Well, your t list contains as many values as the original lst , as you add one item in every iteration of the loop - that gives 512 items.好吧,您的t列表包含与原始lst一样多的值,因为您在循环的每次迭代中添加一个项目 - 这给出了 512 个项目。 However, your pila1 and pila2 lists are extended only for positive values of sinus[i] , ie.但是,您的pila1pila2列表仅针对sinus[i]的正值进行扩展,即。 half as many - so they contain only 256 items.一半 - 所以它们只包含 256 个项目。 To fix it, add items to these list as well (oh, and please do not use extend where append suffices).要修复它,请将项目也添加到这些列表中(哦,请不要在append足够的地方使用extend )。

for i in lst:
    t.append(t_min + lst[i] * T_p)
    sinus.append(mt.sin(2 * mt.pi * frq * t[i]))
    if sinus[i] > 0:
        rect.append(True)
        pila1.append(0) # or whatever value you deem "neutral"
        pila2.append(0)
    else:
        rect.append(False)
        pila1.append((t[i] % (1 / frq)) * frq)
        pila2.append(abs((t[i] % (1 / frq)) * frq - 0.5) * 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3) 谁能帮我? ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (0,) - Can anyone help me? ValueError: x and y must have same first dimension, but have shapes (10,) and (0,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)/ 多处理 - ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)/ Multiprocessing Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题 - ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM