1

I have pandas dataframe with pair of values and like to color code it conditionally such as

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. Any lead is appreciative.

1 Answer 1

4

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>

enter image description here

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')

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>

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

is it possible to nest two conditions in there such as c=np.where(df['a']>0.5 and df['b']<0.5, 'r', 'b') ?
one last thing, can we break the grid into 4 quadrants?
i thought it will be easy to nest np.where but proving to be difficult. I also want to red color value (df['x']<0.5) & (df['y']>0.5) on same plot. including this with another & is throwing error
@Ibe, what exactly are you trying to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.