plotting arrays in python upto a particular element
Question: I have a data file like this:

    
    
    0.001  5.515e-01  1.056e+00  1.384e-01  1.273e+01  -1.808e-01  1.255e+01
    0.002  2.335e-02  -1.100e-03  -8.850e-03  1.273e+01  -3.176e-01  1.241e+01
    0.003  2.335e-02  -1.100e-03  -8.850e-03  1.273e+01  -3.177e-01  1.241e+01
    0.004  2.335e-02  -1.101e-03  -8.851e-03  1.273e+01  -3.177e-01  1.241e+01
    0.005  2.335e-02  -1.101e-03  -8.851e-03  1.273e+01  -3.177e-01  1.241e+01
    0.006  2.335e-02  -1.102e-03  -8.851e-03  1.273e+01  -3.177e-01  1.241e+01
    0.007  2.335e-02  -1.102e-03  -8.852e-03  1.273e+01  -3.177e-01  1.241e+01
    ...    ...          ...         ...        ...         ...        ...
    

where the 1st column is time, the last one is total energy, 2nd last is
potential energy and 3rd last is kinetic energy. Now I want to plot these
energies as function of time, but I do not want to plot the whole array at one
go.

Rather I wish to choose a time and plot the energies upto that time and then
again choose another time and plot the energies upto that time (starting
always from t=0). The code I have written for that is given below:

    
    
    from pylab import*
    from numpy import*
    
    data=loadtxt('500.txt')
    
    t=data[:,0]
    KE=data[:,-3]
    PE=data[:,-2]
    TE=data[:,-1]
    
    t=0
    while t<100:
        ke=KE[:t]
        time=t[:t]
        plot(time,ke)
        picname=temp+'e.png'
        savefig(picname)
        show()
        t=t+40
    

But it returns `File "energyprofile.py", line 14, in <module> time=t[:t]
TypeError: 'int' object has no attribute '__getitem__'`. How can I get round
this problem?


Answer: There is no commas in the [slicing notation for
python](https://docs.python.org/2/tutorial/introduction.html)

this here:

    
    
    t=data[:,0]
    KE=data[:,-3]
    PE=data[:,-2]
    TE=data[:,-1]
    

must be replaced by:

    
    
    t=data[:0]
    KE=data[:-3]
    PE=data[:-2]
    TE=data[:-1]
    

