Python Matplotlib scatter plot: Specify color points depending on conditions
<p>I have two numpy arrays, x and y, with 7000 elements each. I want to make a scatter plot of them giving each point a different color depending on these conditions:</p>

<pre><code>-BLACK if x[i]&lt;10.

-RED if x[i]&gt;=10 and y[i]&lt;=-0.5

-BLUE if x[i]&gt;=10 and y[i]&gt;-0.5 
</code></pre>

<p>I tried creating a list of the same length as the data with the color I want to assign to each point and then plot the data with a loop, but it takes me a long time to run it. Here's my code:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

#color list with same length as the data
col=[]
for i in range(0,len(x)):
    if x[i]&lt;10:
        col.append('k') 
    elif x[i]&gt;=10 and y[i]&lt;=-0.5:
        col.append('r') 
    else:
        col.append('b') 

#scatter plot
for i in range(len(x)):
    plt.scatter(x[i],y[i],c=col[i],s=5, linewidth=0)

#add horizontal line and invert y-axis
plt.gca().invert_yaxis()
plt.axhline(y=-0.5,linewidth=2,c='k')
</code></pre>

<p>Before that, I tried creating the same color list in the same way, but plotting the data without the loop:</p>

<pre><code>#scatter plot
plt.scatter(x,y,c=col,s=5, linewidth=0)
</code></pre>

<p>Even though this plots the data much, much faster than using the for loop, some of the scattered points appear with a wrong color. Why not using a loop to plot the data leads to incorrect color of some points?</p>

<p>I also tried defining three sets of data, one for each color, and adding them to the plot separately. But this is not what I am looking for.</p>

<p>Is there a way to specify in the scatter plots arguments the list of colors I want to use for each point in order not to use the for loop?</p>

<p>PS: This is the plot I get when I don't use  the for loop (wrong one):</p>

<p><a href="https://i.stack.imgur.com/LxlvN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LxlvN.png" alt="enter image description here"></a></p>

<p>And this one when I use the for loop (correct):</p>

<p><a href="https://i.stack.imgur.com/PoCKo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PoCKo.png" alt="enter image description here"></a></p>