【发布时间】:2021-08-08 21:52:19
【问题描述】:
我正在编写一个程序,用户输入一个数学方程(例如 x^2 + 2x + 2),函数 plotFunction 将使用 matplotlib 将其绘制在图表上。 当我输入诸如“x2”之类的参数时,它会返回错误: ValueError:非法格式字符串“x2”;两个标记符号
import matplotlib.pyplot as plt
import numpy as np
class Plotter:
def __init__(self):
pass
def plotFunction(self, func):
x = np.arange(-100, 100)
y = func
plt.plot(x, y)
plt.show()
p1 = Plotter()
p1.plotFunction("x**2")
【问题讨论】:
-
首先欢迎来到 SO。这里有很多问题。您应该学习一些基础知识并查看SymPy 符号数学python 库。你可以用这个库实现你想要的东西。
标签: python matplotlib oop