python


1、ValueError: c of shape (150, 1) not acceptable

<h3>运行出现错误error</h3> <pre><code>ValueError: c of shape (150, 1) not acceptable as a color sequence for x wit</code></pre> <pre><code>import matplotlib.pyplot as plt import numpy as np data = [] label = [] np.random.seed(0) # 以原点为圆心,半径为1的圆把散点划分成红蓝两部分,并加入随机噪音。 for i in range(150): x1 = np.random.uniform(-1,1) x2 = np.random.uniform(0,2) if x1**2 + x2**2 &lt;= 1: data.append([np.random.normal(x1, 0.1),np.random.normal(x2,0.1)]) label.append(0) else: data.append([np.random.normal(x1, 0.1), np.random.normal(x2, 0.1)]) label.append(1) data = np.hstack(data).reshape(-1,2) label = np.hstack(label).reshape(-1, 1) plt.scatter(data[:,0], data[:,1], c = np.squeeze(label), cmap="RdBu", vmin=-.2, vmax=1.2, edgecolor="white") plt.show() </code></pre> <p>修改</p> <pre><code>修改:plt.scatter(data[:,0], data[:,1], c=label,cmap="RdBu", vmin=-.2, vmax=1.2, edgecolor="white")改为:plt.scatter(data[:,0], data[:,1], c=np.squeeze(label),cmap="RdBu", vmin=-.2, vmax=1.2, edgecolor="white")</code></pre> <p>即把<code>c=label</code>改为<code>c=np.squeeze(label)</code></p> <p><a href="https://github.com/lduml/img/blob/master/matplotlib/img_red_blue.png?raw=true"><img src="https://github.com/lduml/img/blob/master/matplotlib/img_red_blue.png?raw=true" alt="" /></a></p>

页面列表

ITEM_HTML