Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - selecting colors within qualitative color map

I am plotting many scatter plots together, e.g.:

import matplotlib.pyplot as plt
import numpy as np

N = 50

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c='blue')

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c='green')

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c='goldenrod')

plt.show()

enter image description here

I am doing this for >10 scatter plots and I would like to choose colors from a qualitative colormap to get color balance & separation, e.g.:

enter image description here

What is the best way to do this?

like image 933
Cactus Philosopher Avatar asked Dec 22 '22 20:12

Cactus Philosopher


1 Answers

I find it pretty neat to use an iterator to be able to select the next color in the list:

import matplotlib.pyplot as plt
import numpy as np

colors = iter([plt.cm.tab20(i) for i in range(20)])

N = 50

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c=[next(colors)])

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c=[next(colors)])

x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c=[next(colors)])

plt.show()

enter image description here

like image 97
Diziet Asahi Avatar answered Jan 11 '23 18:01

Diziet Asahi