user1101010
user1101010

Reputation: 187

How do I alternate the color of a scatter plot in matplotlib?

I have an array of data points of dimension n by 2, where the second dimension $2$ corresponds to the real part and imaginary part of a complex number.

Now I know the data points will intersect the unit circle on the plane for a couple of times. What I want to implement is: suppose the path starts will some color, it changes to another color when it touches the unit circle on the plane and changes color again if it intersects the unit circle again. I am not sure whether there is an easy to implement this.

Upvotes: 0

Views: 2617

Answers (3)

Pam
Pam

Reputation: 26

You may want to try

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = [1,2,3,4]
Y1 = [4,8,12,16]
Y2 = [1,4,9,16]

plt.scatter(X,Y1,color='red')
plt.scatter(X,Y2,color='blue')

plt.show()

Scatter plot 1

or try

x = np.arange(10)
ys = [i+x+(i*x)**2 for i in range(10)]

colors = cm.rainbow(np.linspace(0, 1, len(ys)))

for y, c in zip(ys, colors):
    plt.scatter(x, y, color=c)

plt.show()

Scatter Plot 2

You may also want to check out this thread as well: Setting different color for each series in scatter plot on matplotlib

Upvotes: 1

csg
csg

Reputation: 8186

Try something along the lines of

# Import matplotlib module as plt
import matplotlib.pyplot as plt
import math

x = [3,7,1,9,5,3,5,8,math.sqrt(3)/2]
y = [4,7,8,2,3,4,5,1,1/2]
# Plot scatter Plot
for i in range(len(x)):
    if (round(x[i]**2+y[i]**2,2)) == 1: # equation of unit circle is x^2+y^2=1
        plt.scatter(x[i],y[i], color ='g',marker ='.')
    else:
        plt.scatter(x[i],y[i], color ='r',marker ='*')

plt.xlabel('x')
plt.ylabel('y')
plt.xlim([0,10])
plt.ylim([0,10])
plt.title('Scatter Plot')
plt.legend()
plt.show()

enter image description here

Upvotes: 0

ShlomiF
ShlomiF

Reputation: 2895

The simplest way to do this is probably to implement the logic outside the plot, by assigning a different group to each point defined by your circle-crossing concept.
Once you have these group-indexes it's a simple plt.scatter, using the c (stands for "color") input.

Good luck!

Upvotes: 0

Related Questions