Question

Following a earlier question (here) I'd now like to just plot a point to every grid value which is significant. In the moment I do it like this

ax.contourf(x, y, z)
for i in x:
    for j in y:
        if significant[i, j]==True: ax.plot(i, j, 'bo')

which is obviousliy really bad as it is very slow. Is there some simple (fast) solution for this problem? Note that x and y don't have the same length and I need x to be of variable length.

Was it helpful?

Solution

You can do the loop only where significant is true:

for i, j in zip(*np.where(significant)):
     ax.plot(i, j, 'bo')

Or, if x and y are not arange-like:

for i, j in zip(*np.where(significant)):
     ax.plot(x[i], y[j], 'bo')

np.where will return a tuple with two elements, the first and the second index of the nonzero elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top