有条理地拆分pandas数据帧以绘制不同颜色

问题描述 投票:1回答:1

我有一对带有值的pandas数据帧,并喜欢有条件地对其进行颜色编码,例如

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

但没有到达任何地方。在ab上应用相同的条件是最终目标。任何领导都很感激。

python pandas numpy matplotlib
1个回答
4
投票

演示:

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

更新:

是否有可能在那里嵌套两个条件,如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

© www.soinside.com 2019 - 2024. All rights reserved.