我在循环中调用散点图,并希望每个 plot 使用不同的颜色。

基于这一点,并在你的回答中: 在我看来,你真的希望 N 截然不同的数据集颜色;要映射的整数索引0, 1,.. . N-1 to的颜色。像:

img.php?siteid=1&aid=1494995&hash=djNreEwucG5n

这就是如何用一种通用的方式来完成颜色映射:import matplotlib.pyplot as plt

import matplotlib.cm as cmx

import matplotlib.colors as colors

def get_cmap(N):

'''Returns a function that maps each index in 0, 1,.. . N-1 to a distinct

RGB color.'''

color_norm = colors.Normalize(vmin=0, vmax=N-1)

scalar_map = cmx.ScalarMappable(norm=color_norm, cmap='hsv')

def map_index_to_rgb_color(index):

return scalar_map.to_rgba(index)

return map_index_to_rgb_color

def main():

N = 30

fig=plt.figure()

ax=fig.add_subplot(111)

plt.axis('scaled')

ax.set_xlim([ 0, N])

ax.set_ylim([-0.5, 0.5])

cmap = get_cmap(N)

for i in range(N):

col = cmap(i)

rect = plt.Rectangle((i, -0.5), 1, 1, facecolor=col)

ax.add_artist(rect)

ax.set_yticks([])

plt.show()

if __name__=='__main__':

main()

Logo

一站式 AI 云服务平台

更多推荐