简体   繁体   English

有条件地分割熊猫数据框以使用不同的颜色进行绘制

[英]Split pandas dataframe conditionally to plot with different colors

I have pandas dataframe with pair of values and like to color code it conditionally such as 我有一对带有一对值的pandas数据框,喜欢有条件地对其进行颜色编码,例如

df.plot(kind='scatter', ax=ax1, x='a', y='b', c=np.where(['a']>0.5, 'r', 'g']))

But not getting anywhere. 但是什么也没得到。 Applying same condition on both a and b is ultimate objective. 最终目标是对ab施加相同的条件。 Any lead is appreciative. 任何线索都是有意义的。

Demo: 演示:

In [50]: df = pd.DataFrame(np.random.rand(100, 2), columns=['x','y'])

In [51]: df.head()
Out[51]:
          x         y
0  0.376715  0.209387
1  0.633065  0.212350
2  0.538783  0.883493
3  0.753707  0.983746
4  0.135703  0.840134    

In [52]: df.plot.scatter(x='x', y='y', s=20, c=np.where(df['y']>0.5, 'r', 'g'))
Out[52]: <matplotlib.axes._subplots.AxesSubplot at 0x1078f4e0>

在此处输入图片说明

UPDATE: 更新:

is it possible to nest two conditions in there such as c=np.where(df_AA['a']>0.5 and df_AA['b']<0.5, 'r', 'b') 是否可以在其中嵌套两个条件,例如c=np.where(df_AA['a']>0.5 and df_AA['b']<0.5, 'r', 'b')

In [70]: df.plot.scatter(x='x', y='y', s=20, c=np.where((df['x']>0.5) & (df['y']<0.5), 'r', 'g'), grid=True)
Out[70]: <matplotlib.axes._subplots.AxesSubplot at 0xc166dd8>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM