2015-10-04 116 views
0

我正在尝试创建一系列x,y对的散点图,其中每个系列具有不同的颜色。我的输入是一个形状为2x3x10的三维numpy数组。换句话说:三组不同的10个x,y对。这个最小的例子从一个二元正态分布中生成一对,但正如你所看到的,我在绘制这个系列时的尝试或者不会导致这个系列之间的颜色差异,或者我以许多不同的颜色结束(10也许?)。为了清楚起见,我想以三种不同的颜色结束,一组针对三组10 x,y对中的每一组,或者针对通过阵列的第三维的每个切片的颜色。Matplotlib - 根据系列/子阵列/切片改变颜色

我应该怎么做呢?有没有不同的方式来定义matplotlib的颜色?或者我应该早些时候改变事物并定义多个二维数组而不是3D模型?我对编程相当陌生,对numpy和matplotlib更是如此,但是从我所收集的信息来看,利用数组的多维性来组织事物是一种很好的做法,但是对于这个前沿的任何其他一般指导方法都会很感激。

pairs = np.random.multivariate_normal((1,5),[[1,0],[0,1]],(10,3)).T 

array([[[ 0.49358789, 0.57551098, 2.7029197 , 0.9437744 , -0.45122972, 
      0.05786102, 1.76313729, -0.72469019, 0.53466069, 0.67888213], 
     [ 2.88773234, 1.43831903, -0.7427195 , -0.01451867, 1.56491086, 
      1.72596764, 1.3953636 , 1.67816112, 0.02839967, 0.96014133], 
     [ 2.52065319, -0.2485202 , 1.51877564, 2.31216588, 1.35005209, 
      1.30100189, 0.63590115, 0.32281779, 2.14906114, 0.1551461 ]], 

     [[ 4.85695486, 6.06754 , 5.93342725, 3.49327716, 6.69661302, 
      6.52707216, 4.61195227, 3.22767035, 4.23710242, 7.19532735], 
     [ 5.06087316, 4.29734169, 5.66389379, 4.60574012, 4.96619091, 
      4.88981834, 3.65294396, 5.65582142, 6.27162773, 6.67958156], 
     [ 5.47524034, 4.8989236 , 3.96246028, 6.31088811, 5.39779792, 
      5.67488569, 4.66692489, 4.17364195, 3.69659271, 5.85626402]]]) 

# Note that the actual graphics were based on another random sample 
# than the one listed above, due to a mistake on my end. 

plt.plot(pairs[0],pairs[1],'x');plt.show() 
plt.scatter(pairs[0],pairs[1]);plt.show() 

enter image description here

回答

1
import numpy as np 
import pylab as plt 

pairs = np.random.multivariate_normal((1,5),[[1,0],[0,1]],(10,3)).T 

""" 
pairs = [ 
    [ 
     [ 0.49358789, 0.57551098, 2.7029197 , 0.9437744 , -0.45122972, 
      0.05786102, 1.76313729, -0.72469019, 0.53466069, 0.67888213], 
     [ 2.88773234, 1.43831903, -0.7427195 , -0.01451867, 1.56491086, 
      1.72596764, 1.3953636 , 1.67816112, 0.02839967, 0.96014133], 
     [ 2.52065319, -0.2485202 , 1.51877564, 2.31216588, 1.35005209, 
      1.30100189, 0.63590115, 0.32281779, 2.14906114, 0.1551461 ] 
    ], 

    [ 
     [ 4.85695486, 6.06754 , 5.93342725, 3.49327716, 6.69661302, 
      6.52707216, 4.61195227, 3.22767035, 4.23710242, 7.19532735], 
     [ 5.06087316, 4.29734169, 5.66389379, 4.60574012, 4.96619091, 
      4.88981834, 3.65294396, 5.65582142, 6.27162773, 6.67958156], 
     [ 5.47524034, 4.8989236 , 3.96246028, 6.31088811, 5.39779792, 
      5.67488569, 4.66692489, 4.17364195, 3.69659271, 5.85626402] 
    ] 
] 
""" 

colors = ["red", "green", "blue"] 


# I put the % operator in the color array index so it rolls over 
# to the start of the color array when it runs out of colors 
# (when there's more sets than preset colors) 
for index, group in enumerate(pairs[0]): 
    plt.plot(group, pairs[1][index], "x", color=colors[index % len(colors)]) 
plt.show() 

for index, group in enumerate(pairs[0]): 
    plt.scatter(group, pairs[1][index], color=colors[index % len(colors)]) 
plt.show() 
+0

谢谢,这是非常接近我一直在寻找!现在我想知道,如果我可以自动定义任意长度的随机颜色列表,当我有不止3套。 – Zenit