python - 有条件地拆分 Pandas 数据框以绘制不同的颜色

标签 python pandas numpy matplotlib

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

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

却一事无成。对 ab 应用相同的条件是最终目标。任何线索都是感激的。

最佳答案

演示:

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

更新:

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

关于python - 有条件地拆分 Pandas 数据框以绘制不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102009/

相关文章:

python - 如何在 python 中从头开始获取 kfold 分割以进行交叉验证?

python - IOError : [Errno 2] - Can permissions cause an IOError Errno 2 when using open()

python - 如何在单个Python数组中连接不同类型的特征?

python - 使用 numpy/scipy 识别数字信号的斜率变化?

python - urllib : get utf-8 encoded site source code

python - 如何在 ipython-notebook 中获取 sympy 表达式的 latex 表?

python - 我有 8 年的每日数据。我想绘制一周中每天、一年中的每周和一年中的每个月的所有值。我怎么做?

python - 如何获取箱线图中每个中位数的值?

python - 来自 x-y 点列表的离散傅里叶变换

python - 什么决定了 numpy 数组的数据类型