Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexError: index 1000 is out of bounds for axis 0 with size 1000

I am very new to python and indexing is still difficult for me. I am trying to plot few values using iterative operation but it seems it is not working and giving me above error. Please help me. Thanks.

My code:

import numpy as np
import matplotlib.pyplot as mp

e = np.exp
z = np.arange(1000)


for i in z:
    while True:
    H_2 = 0.58*e(-(z[i]/81)**2)
    i += 1

mp.scatter(H_2 , z, c = 'r')
mp.show()
like image 781
cat Avatar asked Oct 23 '25 12:10

cat


1 Answers

This line is giving you the error i += 1

If you plan on using the while loop, don't forget to add your break statement, otherwise you'll be stuck in an infinite loop. Without any additional details, I don't see why it is necessary in this case.

In addition to that, I would define H_2 as an empty list, and append any values in your calculation to it. According to the documentation, H_2 needs to be an array-like value.

So it should look like:

import numpy as np
import matplotlib.pyplot as mp

e = np.exp
z = np.arange(1000)
H_2 = []

for i in z:
    H_2.append(0.58*e(-(i/81)**2))

mp.scatter(H_2 , z, c = 'r')
mp.show()

Hopefully the graph appears as expected.

like image 120
freshhmints Avatar answered Oct 25 '25 01:10

freshhmints