ValueError: 'c' argument has 100 elements, which is not acceptable for use with 'x' with size 100,

注意matplotlib中画散点图的scatter函数的参数c的用法

c的值不可以是列向量

import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # 1. 数据生成
    N = 100
    x = np.random.uniform(low=-10, high=10, size=(N, 2))
    y = np.dot(x, [[5], [-3]])
    y[y < 0] = 0
    y[y > 0] = 1
    y = y.astype(np.int)
    # print(y)
    '''
    [[0]
     [0]
     [0]
     [0]
     [1]
     [0]
     ...
    '''

    # 2. 可视化
    plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=y)  # s:点的大小  c:点的颜色
    plt.xlabel('x1')
    plt.ylabel('x2')
    plt.show()

此图本程序运行不出来的,此图是上面scatter函数不写参数c的时候运行出来的,但是那样图中两个类别的点是同一种颜色,想换成两种颜色,就得加参数c

报错

ValueError: 'c' argument has 100 elements, which is not acceptable for use with 'x' with size 100, '

把下面一行代码

plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=y)  # s:点的大小  c:点的颜色

改为下面代码即可

plt.scatter(x=x[:, 0], y=x[:, 1], s=10, c=np.squeeze(y))  # s:点的大小  c:点的颜色

对不起,出现了一个错误。这个错误是由于代码中使用了`scatter`函数的`c`参数时,传递的`c`参数的长度与`x`和`y`参数的长度不一致导致的。为了解决这个问题,我们可以将`c`参数设置为一个二维数组,每个像素点的RGB值作为一个数据点。请尝试使用以下代码: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from PIL import Image # 读取图片 image_path = 'path_to_your_image.jpg' # 替换为你的图片路径 image = Image.open(image_path) rgb_image = image.convert('RGB') # 获取图像尺寸和像素值 width, height = image.size pixels = np.zeros((width, height, 3), dtype=np.uint8) for x in range(width): for y in range(height): r, g, b = rgb_image.getpixel((x, y)) pixels[x, y] = [r, g, b] # 构建颜色数组 colors = pixels.reshape(-1, 3) / 255.0 # 构建坐标数组 x_coords, y_coords = np.meshgrid(range(width), range(height)) x_coords = x_coords.flatten() y_coords = y_coords.flatten() # 绘制三维图 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs=x_coords, ys=y_coords, zs=colors[:, 0], c=colors) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('R') plt.show() ``` 请确保将`'path_to_your_image.jpg'`替换为你要读取的图片路径。这段代码将绘制出三维图,其中X轴对应图像的横坐标,Y轴对应图像的纵坐标,Z轴对应R通道的数值。每个像素点的颜色由RGB值表示。希望这次能够成功绘制出图像的三维图!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值