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

在做吴恩达深度学习课程第一课第三周的课后作业时,运行如下代码:

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)

报错:

ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 400, 'y' with size 400.

这是plt.scatter函数微小的改版造成的,只需要修改如下:

import operator
from functools import reduce
plt.scatter(X[0, :], X[1, :], c=reduce(operator.add, Y), s=40, cmap=plt.cm.Spectral) 

添加链接描述

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
对不起,出现了一个错误。这个错误是由于代码中使用了`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值表示。希望这次能够成功绘制出图像的三维图!
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值