我们可以使用Matplotlib库中的pyplot模块来绘制折线图,下面是一个示例代码,用于实现这个类:
import matplotlib.pyplot as plt
class LineGraph:
def __init__(self, x_vals, y_vals):
self.x_vals = x_vals
self.y_vals = y_vals
def plot(self):
"""
根据用户的输入绘制折线图
"""
plt.plot(self.x_vals, self.y_vals)
plt.show()
上面的代码中,我们创建了一个名为LineGraph的类,该类接受x_vals和y_vals作为输入,并将其存储为属性。该类还具有名为plot的方法,该方法将根据用户提供的输入进行折线图的绘制。
为了使用这个类,我们可以这样做:
x_vals = [1, 2, 3, 4, 5]
y_vals = [2, 4, 6, 8, 10]
my_graph = LineGraph(x_vals, y_vals)
my_graph.plot()
在这段代码中,我们首先创建一个x值列表和y值列表。然后,我们使用这些值创建一个LineGraph实例,并调用plot方法,以便根据用户的输入绘制折线图。