Quote:Thank you, it made a little more sense. But if I want to avoid the float,
can I write it in this way:
...
xlist = [i*dx for i in range(-1,1)]
...
range(-1,1)
returns then -1 and 0.
If you want to have more samples between -1 and 1, you have to use floats or use a bigger range.
If you use
range(-1,2)
, it will yield also the 1:
[-1, 0, 1]
You can also change your factor (multiply by 1000) and change your range from (-1,1) to (-1000, 1000) and if you want to include the endpoint, then (-1000, 1001).
I show another example. If you want to calculate the coordinates x and y to draw a circle, you need a range of 2π, cosine for x and sine for y.
from math import sin
from math import cos
from math import tau
# tau == 2*pi
import matplotlib.pyplot as plt
def plot_circle(samples):
pi_factor = tau / (samples - 1)
pi_list = [pi_factor * p for p in range(samples)]
x = [cos(p) for p in pi_list]
y = [sin(p) for p in pi_list]
plt.plot(x,y)
plt.show()
Plotting a circle with 4 samples:
plot_circle(4)
Output:
The resulting list from range(samples) is: 0, 1, 2, 3
To get a circle and not a triangle, you need more points.