Matplotlib颜色分配错误?

时间:2017-08-29 12:50:28

标签: python matplotlib plot scatter-plot

我试图根据数组中的数据制作彩色散点图:

plt.scatter(150, 93, c=y_pred)

此处y_pred为:

array([ 5,  6,  8, 16, 21, 12, 12, 13,  6,  6, 17, 11,  6, 12, 12, 23,  6,
        6, 15,  6,  6,  6,  6,  6,  6, 23, 22,  6, 12, 17,  6, 20,  0,  6,
        6, 12, 12,  0,  6,  6,  6,  6,  6,  6,  5, 17,  6,  6, 11, 10, 13,
        6, 22, 24, 23,  6,  6, 13,  6,  6,  6, 12,  9, 15, 13, 14,  6, 18,
        1,  6,  9,  6,  6, 11,  6,  5, 16,  9, 23,  2, 14, 24,  9,  5,  9,
       10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,  1,  6, 19,  6, 23,  3,
       20, 10,  4,  8,  9,  6,  6,  9, 22, 23,  6,  6, 11,  6,  6,  6, 22,
       24, 14,  4,  7, 12,  6, 19,  6, 12,  3, 22,  6, 11,  6, 21, 23,  4,
        6,  6,  6,  4, 10, 22, 15,  6,  6, 18,  6, 14,  4,  5], dtype=int32)

这给了我一个错误:

  

ValueError:无效的RGBA参数:17

我不明白为什么。相同的解决方案适用于others。你能帮我理解这个错误吗?

1 个答案:

答案 0 :(得分:1)

您只需在x=150 ; y=93添加一个散点,但您尝试为这一个值分配150种颜色。

plt.scatter(150, 93)

enter image description here

如果您传入与x具有相同形状的yc,则会有效:

plt.scatter(np.random.random(150), np.random.random(150), c=y_pred)

enter image description here