2017-02-26 59 views
2

我有这个要求。我在包含每行3个属性的文本文件中有一个示例数据。 Test1的得分,得分的Test2和及格或不及格表示为1或0 例如: -用第三列值绘制熊猫数据框的x和y列确定点的形状

Score1 Score2 Result 
35.00 55.00 0 
45.00 34.00 0 
50.00 75.00 0 
80.00 80.00 1 
55.00 85.00 1 
67.03 66.03 0 
.. 
.. 

现在我想要绘制Score1对X轴和Score2对Y轴,但我要代表通(1)为'+',而当我绘制点和不同的颜色时(例如'绿色'为'绿色',而'红色'为'0'),则失败(0)为'o'

我写下面的代码为如下: -

pos=y[y==1] 
neg=y[y==0] 
get_ipython().magic('matplotlib inline') 

ax=X.plot(kind='scatter',x='Score1',y='Score2',s=pos*10,color='DarkGreen', label='Pass');   
X.plot(kind='scatter', x='Score1', y='Score2', s=neg*200, color='Red', label='Fail',ax=ax); 

我不确定这个权利,因为我只能看到通过结果的情节,但没有t我要求的颜色,而我的失败结果不会被绘制。 我在这里做错了什么?

回答

1

可以使用boolean indexing过滤:

pos=y[y.Result==1] 
neg=y[y.Result==0] 

ax=pos.plot(kind='scatter', 
      x='Score1', 
      y='Score2', 
      s=100, 
      color='DarkGreen', 
      label='Pass', 
      marker='+')  

neg.plot(kind='scatter', 
     x='Score1', 
     y='Score2', 
     s=50, 
     color='Red', 
     label='Fail', 
     marker='o', 
     ax=ax) 

patches, labels = ax.get_legend_handles_labels() 
ax.legend(patches, labels, loc='upper left', scatterpoints=1) 

graph

+0

谢谢。我应该告诉你,Y是一个系列。但是我在X本身上使用了你的代码并且工作。欣赏它。 – sunny

2

使用字典来定义每个结果类型
使用groupby标记通过类型进行迭代

m = {0: 'o', 1: '+'} 
fig, ax = plt.subplots(1, 1) 
for n, g in X.groupby('Result'): 
    g.plot.scatter(
     'Score1', 'Score2', marker=m[n], ax=ax) 

enter image description here

+0

感谢您的回复。如何使用示例代码获得正确的颜色集合? – sunny

+0

@sunny使用我用于标记的相同技巧。使用参数颜色。 – piRSquared