user3151828
user3151828

Reputation: 363

Curve is not continuous in matplotlib for python 3

This is my program to graph functions and it works great. There is only one problem.

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!")

When I try a non-linear function, it is not "curvy":

FUNCTION GRAPHER
Function: y=sin(x**2)
Min X Value: 0
Max X Value: 32

I cannot post a pic because I don't have enough reputation yet... but its just really spiky, and the points are only plotted every 1 unit.

Upvotes: 3

Views: 714

Answers (1)

Spencer Hill
Spencer Hill

Reputation: 899

Rather than range(domainmin,domainmax), use numpy.linspace(domainmin,domainmax, n_pts) where n_pts is the number of points you want, e.g. 200 or some other large number since you want the plot to be less jagged. Documentation for numpy.linspace can be found here.

The function range can only handle integers; see documentation here.

Upvotes: 6

Related Questions