Slicing a graph
Question: I have created a graph in python but I now need to take a section of the graph
and expand this by using a small range of the original data, but I don't know
how to find the row number of the results that form the range or how I can
create a graph using just these results form the file. This is the code I have
for the graph:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    #variable for data to plot
    spec_to_plot = "SN2012fr_20121129.42_wifes_BR.dat"
    
    #tells python where to look for the file
    spec_directory = '/home/fh1u16/Documents/spectra/' 
    data = np.loadtxt(spec_directory + spec_to_plot, dtype=np.float)
    
    x = data[:,0]
    y = data[:,1]
    
    plt.plot(x, y)
    plt.xlabel("Wavelength")
    plt.ylabel("Flux")
    plt.title(spec_to_plot)
    plt.show()
    

edit: data is between 3.5e+3 and 9.9e+3 in the first column, I need to use
just the data between 5.5e+3 and 6e+3 to plot another graph, but this only
applies to the first column. Hope this makes a bit more sense? Python version
2.7


Answer: If I understand you correctly, you could do it this way:

    
    
    my_slice = slice(np.argwhere(x>5.5e3)[0], np.argwhere(x>6e3)[0])
    x = data[my_slice,0]
    y = data[my_slice,1]
    

`np.argwhere(x>5.5e3)[0]` is the index of the first occurrence of `x>5.5e3`
and like wise for the end of the slice. (assuming your data is sorted)

A more general way working even if your data is not sorted:

    
    
    mask = (x>5.5e3) & (x<6e3)
    x = data[mask, 0]
    y = data[mask, 1]
    

