Python Matplotlib-根据系列/子阵列/切片更改颜色
我试图创建一个x,y对的散点图,每个系列都有不同的颜色。我的输入是一个形状为2x3x10的三维numpy数组。换句话说:三组不同的10 x,y对。这个最小的例子从二元正态分布生成成对,但是正如你所看到的,我绘制序列的尝试要么导致序列之间没有颜色差异,要么我最终得到许多不同的颜色(可能是10?)。为了清楚起见,我想以三种不同的颜色结束,三组10 x,y对中的每一组都有一种颜色,或者通过阵列的第三维每个切片都有一种颜色 我该怎么做呢?是否有其他方法定义matplotlib的颜色?或者我应该在前面更改内容并定义多个二维数组而不是三维数组?我对编程相当陌生,对numpy和matplotlib更是如此,但从我收集的资料来看,利用数组的多维性来组织事情是一种很好的做法,但我们非常欣赏这方面的任何其他一般指导原则Python Matplotlib-根据系列/子阵列/切片更改颜色,python,arrays,numpy,matplotlib,Python,Arrays,Numpy,Matplotlib,我试图创建一个x,y对的散点图,每个系列都有不同的颜色。我的输入是一个形状为2x3x10的三维numpy数组。换句话说:三组不同的10 x,y对。这个最小的例子从二元正态分布生成成对,但是正如你所看到的,我绘制序列的尝试要么导致序列之间没有颜色差异,要么我最终得到许多不同的颜色(可能是10?)。为了清楚起见,我想以三种不同的颜色结束,三组10 x,y对中的每一组都有一种颜色,或者通过阵列的第三维每个切片都有一种颜色 我该怎么做呢?是否有其他方法定义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()
谢谢,这与我想要的非常接近!现在我想知道,当我有3组以上的颜色时,是否可以自动定义任意长度的随机颜色列表。
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()