2012-09-02 679 views
95

假设我有三个数据集:设置不同的颜色为每个系列在散点图上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套?

我搜索了这个,可以找到我要问的任何参考。

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

如果我叫散射多次,我只能为每个散射相同的颜色。另外,我知道我可以手动设置颜色数组,但我确信有更好的方法来做到这一点。 我的问题是的话,“我怎样才能自动散点图我的几个数据集,每一个不同的颜色。

是否有帮助,我可以轻松地分配一个唯一的编号给每个数据集。

+1

这是什么问题?颜色也可以是一个数组,但是只要调用多次散布,你不能解决什么问题? – seberg

+1

如果我多次调用scatter,我会得到相同的颜色。我会更新我的问题。 – Yotam

回答

150

我。不知道你说“手动”的意思你可以选择一个colourmap,使彩色阵列很轻松地:

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得到你想要的一个。例如(我懒得打出十个颜色S):

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:我真的很讨厌,我不得不放弃使用“u '使用matplotlib时..]

+1

+1。但是,在这种情况下itertools循环可能不是一个好主意,因为它最终会有多个具有相同颜色的数据集。 –

+1

@DavidRobinson:如果你指定所有十个,不是,但我同意骑自行车排除那里的目的..:^) – DSM

+0

准确地说,那么它不是一个周期:) –

27

在matplotlib中用不同颜色的点绘制图的正常方式是将颜色列表作为参数传递。

例如为:

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

3 colors

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

但是,如果由于某种原因,你想只用一个调用做到这一点,就可以让颜色的大名单,与列表理解和一点地板师:

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) 

All plotted

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])] 
6

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

因为matplotlib.pylab.scatter的当前版本支持赋值:颜色名称字符串数组,颜色地图浮点数数组,RGB或RGBA数组。

这个答案是奉献给@ Oxinabox的修正2013年版的自己无尽的激情在2015年


你必须使用在单一通话多种颜色散射命令两个选项。

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

  2. 早在2013年,无法这样做,因为该命令仅支持整个分散点集合中的单色。当我在做10000线项目时,我想出了一个绕过它的通用解决方案。所以它很俗气,但我可以做任何形状,颜色,大小和透明度。这一招也可以适用于绘制路径集合,线集合....

的代码也由pyplot.scatter源代码的启发,我只是重复而不触发它来绘制做什么分散。

命令pyplot.scatter返回PatchCollection对象,在文件“matplotlib/collections.py”在Collection类私有变量_facecolors和方法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() 
+0

所以它看起来有点复杂,在2013年我用了python 1年。那么为什么人们想知道如何去做呢?得到它的工作后,我再也不用再看它了。我的项目是通过上面的代码绘制出很多可视化的工作流程。 – Hualin

6

您可以随时使用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() 

plot as scatter but changes colors

9

一个简单的办法

您还可以改变颜色af你已经绘制了它们,这有时更容易执行。

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

的输出,让用户可依使用的颜色,甚至当你在同一个插曲许多不同的散点图。

enter image description here

相关问题