2016-05-05 14 views
1

私はパンダ0.18を使用しています。私はこのようなデータフレームがあります。凡例を散布図に追加して色を区別しますか?

code proportion percent_highcost total_quantity 
A81  0.7   76     1002 
A81  0.0   73     1400 

をそして私はこのような散布図を描いています:

colours = np.where(df['proportion'] > 0, 'r', 'b') 
df.plot.scatter(y='percent_highcost', x='total_quantity', c=colours) 

これはうまく動作しますが、私が何を示すのに凡例を追加する方法がわかりません2色が意味する。

私はplt.legend(['Non-dispensing', 'dispensing'], loc=1)を試してみたが、これは奇妙な結果を生成する - 唯一のシリーズがありますので、私は推測する:

enter image description here

誰が助言することはできますか?スキャッタで

+0

を行われていましたアーティストは「散らばっている」。そのアーティストに簡単にアクセスするには、 'df.plot'ではなく' ax.scatter'を使う必要があります。 – tacaswell

答えて

0

プロットユニーク同じ軸上DataFrame S

プロット複数一連(ないpandasSeries)は条件によってDataFrame Sを分離した後、一意の色で別個の飛散としてそれらをプロットすることによって達成することができます同じ軸上にある。これはこのanswerに示されています。私はここにあなたのデータを再現します。

注:これは私が `fig.colorbar(SC)` `sc`があるを使用することをお勧めしiPython/Jupyterノートに

%matplotlib inline 

import pandas as pd 
from cStringIO import StringIO 

# example data 
text = ''' 
code proportion percent_highcost total_quantity 
A81  0.7   76     1002 
A81  0.0   73     1400 
A81  0.1   77     1300 
A81  0.0   74     1200 
A81  -0.1   78     1350 
''' 

# read in example data 
df = pd.read_csv(StringIO(text), sep='\s+') 

print 'Original DataFrame:' 
print df 
print 

# split the DataFrame into two DataFrames 
condition = df['proportion'] > 0 
df1 = df[condition].dropna() 
df2 = df[~condition].dropna() 

print 'DataFrame 1:' 
print df1 
print 

print 'DataFrame 2:' 
print df2 
print 

# Plot 2 DataFrames on one axis 
ax = df1.plot(kind='scatter', x='total_quantity', y='percent_highcost', c='b', s=100, label='Non-Dispensing') 
df2.plot(kind='scatter', x='total_quantity', y='percent_highcost', c='r', s=100, label='Dispensing', ax=ax) 

Original DataFrame: 
    code proportion percent_highcost total_quantity 
0 A81   0.7    76   1002 
1 A81   0.0    73   1400 
2 A81   0.1    77   1300 
3 A81   0.0    74   1200 
4 A81  -0.1    78   1350 

DataFrame 1: 
    code proportion percent_highcost total_quantity 
0 A81   0.7    76   1002 
2 A81   0.1    77   1300 

DataFrame 2: 
    code proportion percent_highcost total_quantity 
1 A81   0.0    73   1400 
3 A81   0.0    74   1200 
4 A81  -0.1    78   1350 

Two Series Scatter Plot

関連する問題