-1

The following code hangs,

while np.any((np.log10((O3 / HB)) < 0.61 / (np.log10(N2 / HA) - 0.05) + 1.3).any()):
    plt.plot(np.array(np.log10(N2 / HA)), np.array(np.log10(O3 / HB)), '.g')
plt.plot(np.array(np.log10(N2 / HA)), np.array(np.log10(O3 / HB)), '.r')

The plot works without the "while" condition. In the above form it hangs. Do I need a "break"?

thanks

Hooked
  • 84,485
  • 43
  • 192
  • 261
rbaer
  • 11
  • 1
  • 3
  • Are you trying to plot values from an array that meet a criteria? I think this is generally done using something like `plt.plot(xdat[bool_array], ydat[bool_array], ...)`. Where you define `bool_array` as something like `bool_array = (ydat > y_thresh) & (xdat < x_thresh)` or something. You don't need a `while` statement to do this. – farenorth Jan 28 '15 at 19:37
  • Alternatively, if you're just trying to plot one point, you probably want to use `if` instead of `while`. – farenorth Jan 28 '15 at 19:39
  • yes, the condition is – rbaer Jan 29 '15 at 09:33
  • yes, the condition is: np.any((np.log10((O3 / HB)) < 0.61 / (np.log10(N2 / HA) - 0.05) + 1.3).any()). Depending on meeting the condition, the plot (several 100 point) is either red or otherwise green. – rbaer Jan 29 '15 at 09:36
  • You need to provide a [minimal complete example](http://stackoverflow.com/help/mcve) of your code. A good place to start would be to provide a minimal example of how `N2`, `HA`, `HB` and `O3` defined? We don't want your entire code. I also suspect that this code-block is inside another loop. Am I correct? – farenorth Jan 29 '15 at 14:47
  • Also, regarding your question 'Do I need a "break"?' The answer is: maybe, but if you're going to put `break` at the end of your `while` block, you should use `if` instead (without a `break`). It does exactly the same thing, and is much better code-style. – farenorth Jan 29 '15 at 14:49
  • O3 = np.array(new_OIII) HB = np.array(new_Hbeta) N2 = np.array(new_NII) HA = np.array(new_Halpha) O1 = np.array(new_OI) S2 = np.array(new_SII) HE2 = np.array(new_HEII) These are the definitions. The code block is not inside a loop. – rbaer Jan 30 '15 at 08:37
  • This is not helpful because I don't know what `new_OIII`, `new_Hbeta`, `new_NII`, `new_Halpha`, `new_OI`, `new_SII` or `new_HEII` are. Again, take a look at the [minimal complete example](http://stackoverflow.com/help/mcve) page for guidance on asking a good question. – farenorth Jan 30 '15 at 14:56

2 Answers2

0

The code probably freezes because you are making a new plot object every time the loop iterates, eventually you'll run out of memory. The program usually spits out a warning after 20 or so plots made.

You can clear the axis each time as an alternative, or

When to use cla(), clf() or close() for clearing a plot in matplotlib?

or you could use the .set_xdata, set_ydata commands to simply update the data. Or you could reuse the same axes if you want to overlay the data.

Community
  • 1
  • 1
Hooked
  • 84,485
  • 43
  • 192
  • 261
0

Discussion of Issue

The code freezes because you are using a while loop with a condition whose variables do not change within the loop. Therefore, if the condition evaluates to True the first time through the loop, it will evaluate to True indefinitely, (i.e. the code 'hangs'). Typically (unless you want the loop to run forever) one or more variables within a while loop will change within the loop. A very simple example is,

while index < 10:
    plt.plot(x_data[index], y_data[index])
    index += 1  # Here the index changes.

Note: For these purposes it is more common to use for index in range(10): or better yet for xd, yd in zip(x_data, y_data): plt.plot(xd, yd) rather than a while loop because then you don't need to have the index += 1 line, and it is clearer at the beginning of the loop what is going on. I merely provided this as a clear example use of while.

Even if the conditional variables do change within the loop (i.e. if you're only providing a snippet of your while loop), it is possible that the conditional statement always evaluates to True. In this case, again, you will never exit the while loop and your code will 'hang'.

Potential Solutions

There are several ways you could fix your problem depending on what you are trying to do:

  1. If you just want to plot your data once, use if instead of while.

  2. If you are only showing us a snippet of your loop and the variables in the conditional statement do change within the loop, I would recommend:

    A) starting by placing a print(np.any((np.log10((O3 / HB)) < 0.61 / (np.log10(N2 / HA) - 0.05) + 1.3).any())) at the top of your loop and run your code. I presume this will dump True to your console.

    B) You could then start to break this statement apart using more print statements such as print('N2: %f' % N2) to understand why your statement is never evaluating to False and therefore you are never exiting the loop.

farenorth
  • 10,165
  • 2
  • 39
  • 45