I am trying to run a simple Python 3 program. I am getting at the moment an index error The code is as shown.
The error reads as below:
Thus the error occurs.
However, what do I do to fix it?
Thanks in advance.
Respectfully,
ErnestTBass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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() |
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