Feb-18-2019, 04:54 PM
I am trying to run a simple Python 3 program. I am getting at the moment an index error The code is as shown.
Thus the error occurs.
However, what do I do to fix it?
Thanks in advance.
Respectfully,
ErnestTBass
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