Creating list np.arange

2015 views python
3

I want to create list, so I used:

x_ticks=np.arange(x1, x2, 6)
print(x_ticks)
print(type(x_ticks))
print(x_ticks[5])

The result is:

[-1.15e-14]
<class 'numpy.ndarray'>
Traceback (most recent call last):
  File "el_motion.py", line 371, in <module>
    print(x_ticks[5])
IndexError: index 5 is out of bounds for axis 0 with size 1

Why there are not 6 elements printed? Thank you

EDIT

How to plot small lines in 3d using this list and for cycle?

x1=-1.15e-14
x2=0.009e-14
x_ticks=np.linspace(x1, x2, 6)
print(x_ticks)
print(type(x_ticks))
print(x_ticks[5])

stepx=(x2-x1)/6
print(stepx)
for xi in range(1, 6):
    ax.plot([xi*stepx, xi*stepx], [-6.3e-15, -6.4e-15], [0,0])

the result is one line that is not on the desired place.

answered question

What are x1 and x2?

The third value is step size, not number of values. Were you thinking of linspace?

2 Answers

6

The 3rd arguments of numpy arange() is step, the number of elements depends on x1, x2, and 6, you might want to print out x_ticks. Examples:

>>> np.arange(0, 24, 6)
array([ 0,  6, 12, 18])
>>> np.arange(0, 30, 6)
array([ 0,  6, 12, 18, 24])

posted this
8

My guess is that the difference between x1 and x2 is less that the provided step interval (6). For example:

np.arange(1, 5)

prints

[1, 2, 3, 4]

whereas for

np.arange(1, 5, 6)

6 is too large of a step, therefore it returns only the first value

[1]

np.linspace(x1, x2, 6) on the other hand, will return x1 one and x2 evenly spaced increments between, resulting in 6 numbers

posted this

Have an answer?

JD

Please login first before posting an answer.