user3361147
user3361147

Reputation: 33

Matplotlib different colour points depending on range of x

I've got a scatter graph created from data I'm using, with plt.plot(), and wondered if there is an easy way to colour the points depending on where they are on the x axis? As in, if they lie between x0 and x1 make them green, x1 and x2 make them blue etc. Any help? Cheers

Upvotes: 1

Views: 482

Answers (1)

mwaskom
mwaskom

Reputation: 49032

You could use plt.scatter:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")

x, y =  np.random.multivariate_normal([0, 0], [(1, .5), (.5, 1)], 200).T
c = np.where(x < 0, "#27ae60", "#2980b9")
plt.scatter(x, y, 30, c)
sns.despine()

enter image description here

Upvotes: 3

Related Questions