2016-10-24 6 views
2

私はパンダにプロットしていたデータフレームを持っている:データフレームが3列パンダのデータフレームの散布図の色分けやラベル付けは?

x y result 
0 2 5 Good 
1 3 2 Bad 
2 4 1 Bad 
3 1 1 Good 
4 2 23 Bad 
5 1 34 Good 

を持って

import pandas as pd 
df = pd.read_csv('Test.csv') 
df.plot.scatter(x='x',y='y') 

私はDF場合は、各ポイントが緑になるように散布図をフォーマットしたいです[ 'result'] = 'Good'、df ['result'] = 'Bad'の場合は赤です。

これはpd.plotを使用して行うことができますか、またはpyplotを使用してそれを行う方法はありますか?

+0

可能な重複http://stackoverflow.com/questions/21654635/scatter -plots-in-pandas-pyplot-how-to-category – johnchase

答えて

3
df.plot.scatter('x', 'y', c=df.result.map(dict(Good='green', Bad='red'))) 

enter image description here

3

1つのアプローチは、同じ軸に2回プロットすることです。最初に "良い"点だけをプロットし、次に "悪い"点をプロットします。トリックは、次のような、scatter方法にaxキーワードを使用することです:

ax = df[df.result == 'Good'].plot.scatter('x', 'y', color='green') 
df[df.result == 'Bad'].plot.scatter('x', 'y', ax=ax, color='red') 

scatter plot

関連する問題