2016-10-23 10 views
0

X = [22,33,55,66,77,55,66,77,44,66,33] Y = [2,6,9,9,10 、5,32,19,123,46,93]プロットが作成されるたびに異なるランダムな色が生成される

リスト= [0,1,2,3,4,0,5,6,7,0,8,9,10,0]

xおよびyはcordinates リストである私は、この例では、異なる色をプロットする必要がリスト用いたアルゴリズムの出力シーケンスを

を表す: 1、2、3、4 - ColorA 5、6、7 - - ColorB 8、9、10 - ColorC または ラインリンクパス: 0,1,2,3,4,0 - ラインA 0、5、6、7、0 - ラインB 0、8、 9、10、0 - lineC

は私は毎回リストランダムな色を生成する機能を必要とし、このコードを

Xsolution = [] 
Ysolution = [] 

for i in range(len(list)): 
    Xsolution.append(X[list[i]]) 
    Ysolution.append(Y[list[i]]) 
    if list[i] == 0 : 
     plt.scatter(Xsolution, Ysolution, color=random_color()) 
     Xsolution = [] 
     Ysolution = [] 

を試みた[i]が0

+0

あなたはその機能をどこに見つけると思いますか? – Matthias

+0

ウェブでランダムな色を生成するが、見つけられなかった関数を探しました。申し訳ありませんが、私はPythonで新しく、私の所見を視覚化するために使用しました –

+1

このような機能はありません。あなたはそれを自分で書く必要があります。 – Matthias

答えて

2
import matplotlib.pyplot as plt 
from random import randint 
import numpy as np 

Xsolution = [] 
Ysolution = [] 

alist = #list of values 

for i in range(len(alist)): 
    Xsolution.append(X[alist[i]]) 
    Ysolution.append(Y[alist[i]]) 

labels = range(1,len(alist)+1) 
i = 0 
fig = plt.figure() 
ax = fig.add_subplot(111) 
for x,y in zip(Xsolution,Ysolution): 
    ax.scatter(x,y,label=labels[i]) 
    if # reached the zero element 
     i = i + 1 #change the label 

#Now this is actually the code that you need, an easy fix to cut and paste. 
colormap = plt.cm.gist_ncar #nipy_spectral, Set1,Paired 
colorst = [colormap(i) for i in np.linspace(0, 0.9,len(ax.collections))]  
for t,j1 in enumerate(ax.collections): 
    j1.set_color(colorst[t]) 


plt.show() 

ある私はこれを取りましたWEBから、THXが必要なものを固定しました

関連する問題