【发布时间】:2021-09-21 06:26:18
【问题描述】:
ValueError: x 和 y 必须具有相同的第一个维度,但具有 (50,) 和 (0,) 形状。 for 循环实例的值未存储在方法 init
中import numpy as np
import matplotlib.pyplot as plt
class inter():
def __init__(self):
self.x = np.array([0, 20, 40, 60, 80, 100], float)
self.y = np.array([20.0, 48.6, 61.6, 71.2, 74.8, 75.2], float)
self.xplt = np.linspace(self.x[0], self.x[-1])
self.yplt = np.array([], float)
self.yp = 0
def loop(self):
for xp in self.xplt:
for xi, yi in zip(self.x, self.y):
self.yp += yi * np.prod((xp - self.x[self.x != xi]) / (xi - self.x[self.x != xi]))
self.yplt = np.append(self.yplt, self.yp)
return self.yplt
def draw(self):
plt.plot(self.xplt, self.yplt)
plt.show()
int = inter()
int.draw()
【问题讨论】:
标签: python arrays matplotlib oop numpy-ndarray