Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

so I'm wondering what the best technique there is to iterate over an array, and then use those values in a matplotlib function, drawgreatcircles.

Say I have a four-column list of values :

-170.13 -16.57 161.63 -77.37
-170.13 -16.57 163.01 -77.575
127.03 -1.047 162.65 -75.075
127.03 -1.047 160.56 -77.28
127.03 -1.047 161.63 -77.37
127.03 -1.047 163.01 -77.575

And I want to iterate over each row, and use those elements in a matplotlib.basemap function, drawgreatcircle. I know my code below is written poorly, where I'm iterating for each x, for each y, etc... In other words, I'm plotting way more great circles than I need, because each x is iterated with each y, and so on.

All I'd like to do is iterate over each row, where each row is read into the drwagreatcircle function and the line is plotted? I've left out the part of my matplotlib basemap script that plots the figure: what's below is just reading in the values that go into drawgreatcircle.

rays = open('ray_temp', 'r')
paths = rays.readlines()
rays.close()

evlo = []
evla = []
stlo = []
stla = []

for i in paths:
    gcarc = i.split()
    evlo.append(float(gcarc[0]))
    evla.append(float(gcarc[1]))
    stlo.append(float(gcarc[2]))
    stla.append(float(gcarc[3]))

EVLO = np.array(evlo)
EVLA = np.array(evla)
STLO = np.array(stlo)
STLA = np.array(stla)

for x in np.nditer(EVLO):
    for y in np.nditer(EVLA):
        for z in np.nditer(STLO):
            for w in np.nditer(STLA):
                map.drawgreatcircle(x,y,z,w)

plt.show()
share|improve this question
    
U r doing sth wrong numpy was created so that u don't hv to use for loops , even when u do try to do it the numpy way , vectorize the function – Lingxiao Xia 10 hours ago

1 Answer 1

Try using zip to perform the iteration.

You could also use np.genfromtxt to read in your file more efficiently, and thus reduce your whole code to:

EVLO, EVLA, STLO, STLA = np.genfromtxt('ray_temp', unpack = True) 

for x,y,z,w in zip(evlo,evla,stlo,stla):
    map.drawgreatcircle(x,y,z,w)

plt.show()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.