对于python 3,matplotlib中的曲线不连续

use*_*828 2 python matplotlib

这是我绘制函数的程序,效果很好.只有一个问题.

while 1==1:
    import numpy as np
    import matplotlib.pyplot as plt
    print("FUNCTION GRAPHER")
    def graph(formula,domain):
        x = np.array(domain)
        y = eval(formula)
        plt.plot(x, y)
        plt.show()
    def sin(x):
        return np.sin(x)
    def cos(x):
        return np.cos(x)
    def tan(x):
        return np.tan(x)
    def csc(x):
        return 1/(np.sin(x))
    def sec(x):
        return 1/(np.cos(x))
    def cot(x):
        return 1/(np.tan(x))
    formula=input("Function: y=")
    domainmin=int(input("Min X Value: "))
    domainmax=int(input("Max X Value: "))
    graph(formula, range(domainmin,domainmax))
    print("DONE!")
Run Code Online (Sandbox Code Playgroud)

当我尝试非线性函数时,它不是"曲线":

FUNCTION GRAPHER
Function: y=sin(x**2)
Min X Value: 0
Max X Value: 32
Run Code Online (Sandbox Code Playgroud)

我不能张贴一张照片,因为我还没有足够的声望......但它只是非常尖刻,并且每1个单位只绘制一个点数.

Spe*_*ill 5

不是range(domainmin,domainmax),用numpy.linspace(domainmin,domainmax, n_pts)其中n_pts的点,你想如200的数量,或其他一些大量的,因为你想的情节是较小的锯齿状.numpy.linspace可在此处找到文档.

该函数range只能处理整数; 看到文档在这里.