绝地无双
一个简单的解决方法如果您只有一种类型的集合(例如,没有误差线的散点图),则还可以在绘制它们后更改颜色,这有时更易于执行。import matplotlib.pyplot as pltfrom random import randintimport numpy as np#Let's generate some random X, Y data X = [ [frst group],[second group] ...]X = [ [randint(0,50) for i in range(0,5)] for i in range(0,24)]Y = [ [randint(0,50) for i in range(0,5)] for i in range(0,24)]labels = range(1,len(X)+1)fig = plt.figure()ax = fig.add_subplot(111)for x,y,lab in zip(X,Y,labels): ax.scatter(x,y,label=lab)您唯一需要的一段代码:#Now this is actually the code that you need, an easy fix your colors just cut and paste not you need ax.colormap = plt.cm.gist_ncar #nipy_spectral, Set1,Paired colorst = [colormap(i) for i in np.linspace(0, 0.9,len(ax.collections))] for t,j1 in enumerate(ax.collections): j1.set_color(colorst[t])ax.legend(fontsize='small')即使在同一子图中有许多不同的散点图,输出也会为您提供不同的颜色。