Matplotlib:RGBA值应在0-1范围内 [英] Matplotlib: RGBA values should be within 0-1 range

查看:114
本文介绍了Matplotlib:RGBA值应在0-1范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想散布一些数据点.

我写道:

sumy = sum(np.unique(y_train))+1
yy = y_train/sumy


plt.scatter(
    X_lda.iloc[:,0],
    X_lda.iloc[:,1],
    c=yy,
    cmap='rainbow')

/usr/local/lib/python3.6/dist-packages/matplotlib/colors.py in to_rgba_array(c, alpha)
    277             result[mask] = 0
    278         if np.any((result < 0) | (result > 1)):
--> 279             raise ValueError("RGBA values should be within 0-1 range")
    280         return result
    281     # Handle single values.

ValueError: RGBA values should be within 0-1 range

y_train 是 0.0 到 9.0 之间的整数,被认为是我想用作该点颜色的每个数据的类别.如您所见,我什至尝试按照要求将其标准化为 0-1,但它仍然会引发错误.

y_train is an integer between 0.0 to 9.0 and is considered to be the class of each data which I want to use as the color of that point. As you see, I even tried to normalize it between 0-1 as requested but it still throws an error.

推荐答案

以下解决了问题:

matplotlib 无法直接处理分类变量.因此,我们将每个类编码为数字,以便将类标签合并到绘图中.

matplotlib can’t handle categorical variables directly. Thus, we encode every class as a number so that we can incorporate the class labels into our plot.

从sklearn.preprocessing导入

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y_train)
plt.xlabel('LD1')
plt.ylabel('LD2')
plt.scatter(
    X_lda.iloc[:,0].real,
    X_lda.iloc[:,1].real,
    c=y,
    cmap='rainbow',
    alpha=0.7)

这篇关于Matplotlib:RGBA值应在0-1范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆