x and y must have same first dimension, but have shapes (181,) and (518,)报错怎么解决?
数据里面的x和y列的个数都是一样的,实在不知道怎么改?二维折线图



x and y must have same first dimension, but have shapes (181,) and (518,)报错怎么解决?
数据里面的x和y列的个数都是一样的,实在不知道怎么改?二维折线图



关注让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言问题分析: 错误提示显示x和y的第一维度不同,但是它们的形状分别是(181,)和(518,)。这意味着数据中的x和y列的长度不同,这导致了代码无法处理。 解决方案: 可以通过将y数据重新采样(如降采样或插值)来匹配x的长度,或者将x数据重采样来匹配y的长度。下面给出两个案例。 重采样y数据: import numpy as np import matplotlib.pyplot as plt
x = np.linspace(0, 10, 181) y = np.sin(x) + np.random.normal(0, 0.1, 518)
y_resampled = np.interp(x, np.linspace(0, 10, 518), y)
plt.plot(x, y_resampled) plt.show() 重采样x数据: import numpy as np import matplotlib.pyplot as plt
x = np.linspace(0, 10, 181) y = np.sin(x) + np.random.normal(0, 0.1, 518)
x_resampled = np.linspace(0, 10, 518) y_resampled = np.interp(x_resampled, x, y)
plt.plot(x_resampled, y_resampled) plt.show()