Python help using 'numpy.masked_where' conditionally from another list
<p>I am writing an astronomy observation simulation. I have a data array which is 1 measurement per 24 hours:</p>

<pre><code>data = [0,1,2,3,4]
</code></pre>

<p>And I have a array which is the minutes of cloud/rain in a 24 hour period, rounded to the nearest hour:</p>

<pre><code>weather = [0,60,120,180,0]
</code></pre>

<p>I want to use a masked array to hide the values in the data array based off the values in the weather array. Masking is important (instead of deleting) for plotting and data analysis further down the line.</p>

<p>So if I want to only show data points where there was &lt; 120 mins of downtime I do:</p>

<pre><code>downtime = 120

data_masked = np.ma.masked_where(weather &lt; downtime, data)
</code></pre>

<p>This should result in:</p>

<pre><code>data_masked = [0, 1, ---, ---, 4]
</code></pre>

<p>But my data_masked seems to be doing the opposite. If I plot both my data and weather on the same axis. I am masking the points where the weather downtime is <em>below</em> my threshold.</p>

<p>I've tried inverting the operator which just seems to keep everything in. Any ideas or am I missing the point of numpy.ma?</p>

<p>Thanks!</p>

<p>Using <code>numpy.ma</code> returns a mask. That is, it <strong>hides</strong> (masks) everything that fits the condition <code>weather &lt; downtime</code>. If you want to <strong>show</strong> everything that fits the condition, just invert the condition:</p>

<pre><code>data = np.array([0,1,2,3,4])
weather = np.array([0,60,120,180,0])
downtime = 120
data_masked = np.ma.masked_where(weather &gt;= downtime, data)
data_masked
</code></pre>

<p>Then the output will be:</p>

<pre><code>masked_array(data = [0 1 -- -- 4], 
             mask = [False False  True  True False],
             fill_value = 999999)
</code></pre>

<p>To access the data, use <code>data_masked.data</code>.</p>

python arrays numpy masked-array