Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index Error
#1
I am trying to run a simple Python 3 program. I am getting at the moment an index error The code is as shown.


import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

topo = np.loadtxt('topo.asc',delimiter=',')

plt.plot(topo[0,:], label='North')

plt.plot(topo[-1,:],'r--', label='South')

plt.plot(topo[len(topo)/2,:], 'g:', linewidth=3, label='Mid')

plot.title('Topographic profiles')
plot.ylabel('Elevation (m)')
plot.xlabel('<-- West  East -->')
plot.legend(loc = 'lower left')

plt.show()
The error reads as below:
Error:
IndexError Traceback (most recent call last) <ipython-input-4-fba3d9a19be8> in <module> 9 plt.plot(topo[-1,:],'r--', label='South') 10 ---> 11 plt.plot(topo[len(topo)/2,:], 'g:', linewidth=3, label='Mid') 12 13 plot.title('Topographic profiles') IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Now I think I see what is wrong.I just do not know how to fix it. In line 11 there is a division by 2. There it it is possible that the index could not be an integer.

Thus the error occurs.

However, what do I do to fix it?

Thanks in advance.

Respectfully,

ErnestTBass
Reply
#2
Integer division (the // operator). Note that it truncates the result, which may or may not be what you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Sure, I found that out. But, I am unsure as how to
fix his error.

How to fix?

Respectfully,

ErnestTBass
Reply
#4
You fix it by replacing the standard division in line 11 with integer division.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks for your help. I will give it try.

Respectfully,

ErnestTBass
Reply
#6
You fix it by replacing the standard division in line 11 with integer division.




I am too new to Python 3 to know how to do that. I understand the concept, but I just
do not know the syntax. I could do it i other languages, but not Python.

Any help appreciated. Thanks in advance.

Respectfully,

ErnestTBass
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 974 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 762 May-15-2023, 03:28 PM
Last Post: buran
  I'm getting a String index out of range error debian77 7 2,332 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,302 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,914 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 3,016 May-22-2021, 11:04 AM
Last Post: supuflounder
  index error surim 4 2,506 Dec-05-2020, 02:34 PM
Last Post: deanhystad
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,060 Nov-13-2020, 07:43 AM
Last Post: buran
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,008 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  List index out of range error while accessing 2 lists in python K11 2 2,103 Sep-29-2020, 05:24 AM
Last Post: K11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020