擅长:python、mysql、java
<p>您刚刚犯了一个小的复制粘贴错误。
只是对你的风格的一个评论:在使用颜色列表时,你可以避免这么多的ifs,所以:</p>
<pre><code>colors=[red,green,blue,black]
</code></pre>
<p>然后:</p>
<pre><code>plt.annotate('', xy=(x, y), xytext=(0, 0), color=colors[max(3,label)] , textcoords='offset points')
</code></pre>
<p>你的代码必须如此,你总是写<code>elif label=1</code>,这完全没有意义:</p>
<pre><code>import matplotlib.pyplot as plt
Y = [ 1 , 2 , 3 ]
X = [ 1 , 2 , 4 ]
vocabulary = [1 , 2 , 3]
plt.scatter(X , Y)
for label, x, y in zip(vocabulary, X, Y):
if(label == 1):
plt.annotate('', xy=(x, y), xytext=(0, 0), color='red' , textcoords='offset points')
elif(label == 2):
plt.annotate('', xy=(x, y), xytext=(0, 0), color='green' , textcoords='offset points')
elif(label == 3):
plt.annotate('', xy=(x, y), xytext=(0, 0), color='blue' , textcoords='offset points')
else :
plt.annotate('', xy=(x, y), xytext=(0, 0), color='black' , textcoords='offset points')
plt.show()
</code></pre>