2017-01-04 5 views
1

私は約9800エントリのデータセットを持っています。 1つの列にはユーザー名(約60個のユーザー名)が含まれています。私はmatplotlibで散布図を生成し、異なるユーザーに異なる色を割り当てたいと思います。matplotlibを使って散布図に色を自動的に割り当てますか?

これは私が何をすべきか、基本的である:

import matplotlib.pyplot as plt 
import pandas as pd 

x = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30] 
y = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600] 
users =['mark', 'mark', 'mark', 'rachel', 'rachel', 'rachel', 'jeff', 'jeff', 'jeff', 'lauren', 'lauren', 'lauren'] 

#this is how the dataframe basicaly looks like  
df = pd.DataFrame(dict(x=x, y=y, users=users) 

#I go on an append the df with colors manually 
#I'll just do it the easy albeit slow way here 

colors =['red', 'red', 'red', 'green', 'green', 'green', 'blue', 'blue', 'blue', 'yellow', 'yellow', 'yellow'] 

#this is the dataframe I use for plotting 
df1 = pd.DataFrame(dict(x=x, y=y, users=users, colors=colors) 

plt.scatter(df1.x, df1.y, c=df1.colors, alpha=0.5) 
plt.show() 

しかし、私は手動でユーザーに色を割り当てる必要はありません。私は数週間のうちにこれを何度もやらなければならず、ユーザーは毎回違うものになるだろう。

私は2つの質問がある:

(1)個々のユーザーに自動的に色を割り当てる方法はありますか? (2)そうであれば、配色やパレットを割り当てる方法はありますか?

+0

可能な重複を使用して配列を持っている[パンダ/ Pyplotで散布図:カテゴリ別にプロットする方法](http://stackoverflow.com/質問/ 21654635 /散布図 - パンダ - パイロット - どのようにプロット - カテゴリ別) – tom

+0

@トム私はそうは思わない。データフレームにカラー列を動的に割り当てる方法が必要です。あなたが提案する質問は、色ではなくグループ化されたプロットに関連しています。 – Rachel

答えて

2

次に、各ユーザーに1つの固有の色が付いた辞書(user_colors)があります。

colors = [user_colors[user] for user in users] 

今、あなたは別個の、ユーザーごとに色

+0

ありがとう!私はあなたがしていることを理解していると思います。しかし、それをパンダのデータフレームにも適用できますか?それはどのように機能しますか? – Rachel

関連する問題