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

我怎样才能用10套呢?

我search了这个,可以find我要问的任何参考。

编辑:澄清(希望)我的问题

如果我多次调用散点图,我只能在每个散点图上设置相同的颜色。 此外,我知道我可以手动设置颜色数组,但我相信有一个更好的方法来做到这一点。 我的问题是,“我怎样才能自动分散绘制我的几个数据集,每个数据集都有不同的颜色。

如果有帮助,我可以很容易地为每个数据集分配一个唯一的编号。

我不知道你的意思是“手动”。 您可以select一个色彩映射并且足够容易地创build一个色彩数组:

 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) 

或使用itertools.cycle制作自己的色循环器,并指定要循环的颜色,然后使用next来获得所需的颜色。 例如(我懒得输出十种颜色):

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

来想一想,也许它更清洁,不要使用与第一个zip

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

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

在matplotlib中用不同颜色的点绘制阴谋的正常方法是传递一个颜色列表作为参数。

例如:

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

3种颜色

当你有一个列表的列表,并希望他们每个列表着色。 我认为最优雅的方式是由@DSMbuild议,只是做一个循环,使多个调用分散。

但是,如果因为某种原因,只想打一个电话就可以做到,你可以制作一个大的颜色清单,有一个列表理解和一个​​地板部门:

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

即使在同一子图中有多个不同的散点图,输出也会给出不同的颜色。

在这里输入图像说明

这个问题在2013年1月之前有点棘手,matplotlib 1.3.1(2013年8月),这是您可以在matpplotlib网站上find的最老的稳定版本。 但之后,这是相当微不足道的。

因为当前版本的matplotlib.pylab.scatter支持赋值:颜色名称string数组,带有颜色映射的浮点数数组,RGB或RGBA数组。

这个答案是致力于@ Oxinabox对纠正2013年版本自己的无尽激情。


在一次调用中,您有两个使用多种颜色的scatter命令的选项。

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

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

代码也是由pyplot.scatter的源代码所pyplot.scatter ,我只是重复了什么分散而没有触发它绘制。

命令pyplot.scatter返回一个PatchCollection对象,在文件“matplotlib / collections.py”中一个私有variables_facecolorsCollection类和一个方法set_facecolors

所以每当你有一个分散点绘制你可以做到这一点:

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

你总是可以像这样使用plot()函数:

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

散点图但改变颜色