简体   繁体   中英

Matplotlib - selecting colors within qualitative color map

I am plotting many scatter plots together, eg:

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()

在此处输入图片说明

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

在此处输入图片说明

What is the best way to do this?

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM