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()
formula=input("Function: ")
domainmin=float(input("Min X Value: "))
domainmax=float(input("Max X Value: "))
graph(formula, range(domainmin,domainmax))
This is my code to create a grapher. As you can see the user can type in the function, and the domain. I am testing it with a simple y=x function, and this is the Error I get. I'm guessing it has something to do with how I set up the input.
FUNCTION GRAPHER
Function: x
Min X Value: -1
Max X Value: 10
Traceback (most recent call last):
File "/Users/William/Documents/Science/PYTHON/Grapher.py", line 12, in <module>
graph(formula, range(domainmin,domainmax))
TypeError: 'float' object cannot be interpreted as an integer
After the error occurs I type this:
graph('x', range(-1,10))
And the graph pops up. I'm using Python 3.4.
From the documentation:
The arguments to the range constructor must be integers
So to use range
in this case you need to change your code to this:
domainmin=int(input("Min X Value: "))
domainmax=int(input("Max X Value: "))
Or, perhaps closer to what you intend, you can use instead numpy.arange
or numpy.linspace
, so you can use float
ranges - you will have to additionally add granulation parameter (step
for arange
) or number of steps (num
for linspace
).