pytorch利用 scatter画散点图报错 :ValueError: c of shape (200, 1) not acceptable as a color sequence for x with size 200, y with size 200
报错的全部内容为?File "D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 223, in _to_rgba_no_colorcycleraise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4During handling of the above exception, another exception occurred:Traceback (most recent call last):File "E:/python/pyTorch/Learn_0.4_Pytorch/classify/classify.py", line 22, in plt.scatter(x_data.numpy()[:,0], x_data.numpy()[:,1], c=y_data.numpy(), s=200, lw=0, cmap='RdYlGn')File "D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 3470, in scatteredgecolors=edgecolors, data=data, **kwargs)File "D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1855, in innerreturn func(ax, *args, **kwargs)File "D:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 4279, in scatter.format(c.shape, x.size, y.size))
ValueError: c of shape (200, 1) not acceptable as a color sequence for x with size 200, y with size 200Process finished with exit code 1
解决办法:
将代码:plt.scatter(x_data.numpy()[:,0], x_data.numpy()[:,1], c=y_data.numpy(), s=200, lw=0, cmap='RdYlGn')
修改为:plt.scatter(x_data.numpy()[:,0], x_data.numpy()[:,1],c=np.squeeze(y_data.numpy()), s=200, lw=0, cmap='RdYlGn')Note: 别忘记导入 numpy ,即需要在代码开头有这样一句代码:import numpy as np