在matplotlib散点图中为每个系列设置不同的颜色

``X = [1,2,3,4] Y1 = [4,8,12,16] Y2 = [1,4,9,16]` `

` `from matplotlib import pyplot as plt plt.scatter(X,Y1,color='red') plt.scatter(X,Y2,color='blue') plt.show()` `

5 Solutions collect form web for “在matplotlib散点图中为每个系列设置不同的颜色”

` `import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm x = np.arange(10) ys = [i+x+(i*x)**2 for i in range(10)] colors = cm.rainbow(np.linspace(0, 1, len(ys))) for y, c in zip(ys, colors): plt.scatter(x, y, color=c)` `

` `colors = itertools.cycle(["r", "b", "g"]) for y in ys: plt.scatter(x, y, color=next(colors))` `

` `colors = iter(cm.rainbow(np.linspace(0, 1, len(ys)))) for y in ys: plt.scatter(x, y, color=next(colors))` `

[PS：我真的很讨厌在使用matplotlib的时候不得不放弃'u'..]

` `import matplotlib.pyplot matplotlib.pyplot.scatter([1,2,3],[4,5,6],color=['red','green','blue'])` `

` `import matplotlib import numpy as np X = [1,2,3,4] Ys = np.array([[4,8,12,16], [1,4,9,16], [17, 10, 13, 18], [9, 10, 18, 11], [4, 15, 17, 6], [7, 10, 8, 7], [9, 0, 10, 11], [14, 1, 15, 5], [8, 15, 9, 14], [20, 7, 1, 5]]) nCols = len(X) nRows = Ys.shape[0] colors = matplotlib.cm.rainbow(np.linspace(0, 1, len(Ys))) cs = [colors[i//len(X)] for i in range(len(Ys)*len(X))] #could be done with numpy's repmat Xs=X*nRows #use list multiplication for repetition matplotlib.pyplot.scatter(Xs,Ys.flatten(),color=cs)` `

` `cs = [array([ 0.5, 0. , 1. , 1. ]), array([ 0.5, 0. , 1. , 1. ]), array([ 0.5, 0. , 1. , 1. ]), array([ 0.5, 0. , 1. , 1. ]), array([ 0.28039216, 0.33815827, 0.98516223, 1. ]), array([ 0.28039216, 0.33815827, 0.98516223, 1. ]), array([ 0.28039216, 0.33815827, 0.98516223, 1. ]), array([ 0.28039216, 0.33815827, 0.98516223, 1. ]), ... array([ 1.00000000e+00, 1.22464680e-16, 6.12323400e-17, 1.00000000e+00]), array([ 1.00000000e+00, 1.22464680e-16, 6.12323400e-17, 1.00000000e+00]), array([ 1.00000000e+00, 1.22464680e-16, 6.12323400e-17, 1.00000000e+00]), array([ 1.00000000e+00, 1.22464680e-16, 6.12323400e-17, 1.00000000e+00])]` `

一个简单的修复

` `import matplotlib.pyplot as plt from random import randint import 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')` `

1. 作为`pylab.scatter`命令支持使用RGBA数组来做任何你想要的颜色;

2. 早在2013年初，就没有办法这样做了，因为命令只支持整个分散点集合中的单色。 当我在做10000线项目时，我想出了一个通用的解决scheme来绕过它。 所以它很俗气，但我可以做任何形状，颜色，大小和透明。 这招也可以适用于绘制path收集，线路收集….

` `# rgbaArr is a N*4 array of float numbers you know what I mean # X is a N*2 array of coordinates # axx is the axes object that current draw, you get it from # axx = fig.gca() # also import these, to recreate the within env of scatter command import matplotlib.markers as mmarkers import matplotlib.transforms as mtransforms from matplotlib.collections import PatchCollection import matplotlib.markers as mmarkers import matplotlib.patches as mpatches # define this function # m is a string of scatter marker, it could be 'o', 's' etc.. # s is the size of the point, use 1.0 # dpi, get it from axx.figure.dpi def addPatch_point(m, s, dpi): marker_obj = mmarkers.MarkerStyle(m) path = marker_obj.get_path() trans = mtransforms.Affine2D().scale(np.sqrt(s*5)*dpi/72.0) ptch = mpatches.PathPatch(path, fill = True, transform = trans) return ptch patches = [] # markerArr is an array of maker string, ['o', 's'. 'o'...] # sizeArr is an array of size float, [1.0, 1.0. 0.5...] for m, s in zip(markerArr, sizeArr): patches.append(addPatch_point(m, s, axx.figure.dpi)) pclt = PatchCollection( patches, offsets = zip(X[:,0], X[:,1]), transOffset = axx.transData) pclt.set_transform(mtransforms.IdentityTransform()) pclt.set_edgecolors('none') # it's up to you pclt._facecolors = rgbaArr # in the end, when you decide to draw axx.add_collection(pclt) # and call axx's parent to draw_idle()` `

` `import matplotlib.pyplot as plt import numpy as np x = np.arange(10) ys = [i+x+(i*x)**2 for i in range(10)] plt.figure() for y in ys: plt.plot(x, y, 'o') plt.show()` `

• 我如何使用numpy.correlate做自相关？
• Scrapy可以用来从使用AJAX的网站上抓取dynamic内容吗？
• 如何在Python中逃避os.system（）调用？
• Python中可转换的STFT和ISTFT
• 是否有可能使用Python来为iOS和Android编写跨平台的应用程序？
• 如何获得在Django中具有特定权限组的所有用户的列表
• Python - 查找数字列表中最大的数字
• Python - 一个字典慢find每个字符的频率？
• 规范Unicode
• 你如何检查python是否一个string只包含数字？
• 如何克隆一个Python生成器对象？