【发布时间】:2020-02-16 20:51:46
【问题描述】:
我需要一个程序,它可以使用我在控制台中编写的函数通过 matplotlib 制作图表。 但它不适用于三角函数。 我已经写的代码是:
from numpy import linspace
import matplotlib.pyplot as plt
from math import sin, cos, tan
print("input a:")
a = float(input())
print("input b:")
b = float(input())
x = linspace(a, b, 1001)
y = eval(input())
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
【问题讨论】:
-
从
numpy而不是math导入你的函数,即from numpy import linspace, sin, cos, tan,这样它们就可以接受向量输入。 -
不要只说“它不起作用”!显示错误。其他人猜测您在使用带有
numpy数组x的math.sin时遇到问题。在这种情况下,错误消息将说明有关创建 Python 标量的内容。
标签: python numpy matplotlib eval trigonometry