python - Change color of matplotlib.pyplot points -
here plot code i've written :
import matplotlib.pyplot plt y = [ 1 , 2 , 3 ] x = [ 1 , 2 , 4 ] vocabulary = [1 , 2 , 3] plt.scatter(x , y) 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 == 1): plt.annotate('', xy=(x, y), xytext=(0, 0), color='green' , textcoords='offset points') elif(label == 1): 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()
i'm attempting change color depending on value in array vocabulary
if 1 color data point red , if 2 green , if 3 blue else color point black. points color of each point set blue. how color data point depending on current value of vocabulary
?
above code produces :
you made little copy & paste error. comment style : avoid many ifs when using list of colors , :
colors=[red,green,blue,black]
and :
plt.annotate('', xy=(x, y), xytext=(0, 0), color=colors[max(3,label)] , textcoords='offset points')
your code has so, wrote elif label=1
, makes absolutely no sense:
import matplotlib.pyplot plt y = [ 1 , 2 , 3 ] x = [ 1 , 2 , 4 ] vocabulary = [1 , 2 , 3] plt.scatter(x , y) 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()
Comments
Post a Comment