Python matplotlib散布失败,错误为:';c';参数有n个元素,不能与';x';尺寸为n';y';尺寸为n的
我正在尝试使用matplotlib创建散点图,其中每个点都有特定的颜色值 我缩放这些值,然后在“左”和“右”颜色之间应用alpha混合Python matplotlib散布失败,错误为:';c';参数有n个元素,不能与';x';尺寸为n';y';尺寸为n的,python,matplotlib,scatter-plot,Python,Matplotlib,Scatter Plot,我正在尝试使用matplotlib创建散点图,其中每个点都有特定的颜色值 我缩放这些值,然后在“左”和“右”颜色之间应用alpha混合 # initialization from matplotlib import pyplot as plt from sklearn.preprocessing import MinMaxScaler import numpy as np values = np.random.rand(1134) # actual code colorLeft = np.a
# initialization
from matplotlib import pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import numpy as np
values = np.random.rand(1134)
# actual code
colorLeft = np.array([112, 224, 112])
colorRight = np.array([224, 112, 112])
scaled = MinMaxScaler().fit_transform(values.reshape(-1, 1))
colors = np.array([a * colorRight + (1 - a) * colorLeft for a in scaled], dtype = np.int64)
# check values here
f, [sc, other] = plt.subplots(1, 2)
sc.scatter(np.arange(len(values)), values, c = colors)
但是,最后一行给出了错误:
“c”参数有1134个元素,不能与大小为1134的“x”和大小为1134的“y”一起使用
参数c的值
c:颜色、顺序或颜色顺序,可选
标记颜色。可能值:
A single color format string.
A sequence of color specifications of length n.
A sequence of n numbers to be mapped to colors using cmap and norm.
A 2-D array in which the rows are RGB or RGBA.
其中我想使用最后一个选项和RGB值
我将此处的检查值
注释替换为一些打印语句:
print(values)
print(colors)
print(values.shape)
print(colors.shape)
结果如下:
[0.08333333 0.08333333 0.08333333 ... 1. 1. 1.08333333]
[[112 224 112]
[112 224 112]
[112 224 112]
...
[214 121 112]
[214 121 112]
[224 111 112]]
(1134,)
(1134, 3)
将颜色转换为带0的浮点数组如果我的生活是一本侦探小说,那么错误消息将是一条红鲱鱼。。。