如何根据条件更改代码中一系列值的颜色

How do I change the colour for a range of values in the code based on the condition

所以我有一个看起来像这样的数据集

        ACCx    ACCy    ACCz    ECG        RESP    LABEL    BINARY
0   0.9554  -0.2220 -0.5580 0.021423    -1.148987   0.0     0
1   0.9258  -0.2216 -0.5538 0.020325    -1.124573   0.0     0
2   0.9082  -0.2196 -0.5392 0.016525    -1.152039   0.0     0
3   0.8974  -0.2102 -0.5122 0.016708    -1.158142   0.0     0
4   0.8882  -0.2036 -0.4824 0.011673    -1.161194   0.0     0
... ... ... ... ... ... ... ...
695 0.9134  -0.1400 0.1074  0.003479    2.299500    7.0     0
696 0.9092  -0.1394 0.0994  0.000778    2.305603    7.0     0
697 0.9084  -0.1414 0.0934  -0.001694   2.297974    7.0     0
698 0.9116  -0.1416 0.0958  -0.003799   2.354431    7.0     0
699 0.9156  -0.1396 0.1022  -0.006546   2.355957    7.0     0

现在如果LABEL为2,Binary的值为1,如下图

        ACCx    ACCy    ACCz    ECG        RESP    LABEL    BINARY
200 0.8776  -0.1030 -0.2968 -0.011673   -1.222229   2.0     1
201 0.8758  -0.1018 -0.2952 -0.001556   -1.202393   2.0     1
202 0.8760  -0.1030 -0.2918 0.022385    -1.222229   2.0     1
203 0.8786  -0.1038 -0.2950 0.049622    -1.228333   2.0     1
204 0.8798  -0.1050 -0.2930 0.084457    -1.210022   2.0     1
... ... ... ... ... ... ... ...
295 0.8756  -0.1052 -0.2694 -0.106430   -0.883484   2.0     1
296 0.8760  -0.1036 -0.2680 -0.108719   -0.880432   2.0     1
297 0.8760  -0.1056 -0.2638 -0.106750   -0.888062   2.0     1
298 0.8768  -0.1064 -0.2560 -0.099792   -0.889587   2.0     1
299 0.8792  -0.1064 -0.2510 -0.094894   -0.865173   2.0     1

我需要根据 RESP 值绘制散点图,但二进制为 1 的值的颜色必须不同

我使用以下代码绘制了散点图

def plot_coloured(dataframe):
"""
    Function 2: plot_coloured(dataframe)
    Parameters: dataframe: Stress data DataFrame
    Output: Plot
"""
plt.figure(figsize=(12, 6))
plt.scatter(x=[i for i in range(0, 700)],
            y=dataframe["RESP"])

并得到了下图 The image for scatterplot between resp and indices

我想知道如何更改二进制值为 1 的点的颜色 我听说过 plt,scatter() 中的 c 参数,但我不知道它是否对这里有帮助

  • 使用布尔掩码根据所需条件创建单独的数据帧,然后用不同颜色绘制两个数据帧
import pandas as pd
import matplotlib.pyplot as plt

data = {'ACCx': [0.9554, 0.9258, 0.9082, 0.8974, 0.8882, 0.9134, 0.9092, 0.9084, 0.9116, 0.9156, 0.8776, 0.8758, 0.876, 0.8786, 0.8798, 0.8756, 0.876, 0.876, 0.8768, 0.8792],
        'ACCy': [-0.222, -0.2216, -0.2196, -0.2102, -0.2036, -0.14, -0.1394, -0.1414, -0.1416, -0.1396, -0.103, -0.1018, -0.103, -0.1038, -0.105, -0.1052, -0.1036, -0.1056, -0.1064, -0.1064],
        'ACCz': [-0.558, -0.5538, -0.5392, -0.5122, -0.4824, 0.1074, 0.0994, 0.0934, 0.0958, 0.1022, -0.2968, -0.2952, -0.2918, -0.295, -0.293, -0.2694, -0.268, -0.2638, -0.256, -0.251],
        'ECG': [0.021422999999999998, 0.020325, 0.016525, 0.016708, 0.011673000000000001, 0.003479, 0.000778, -0.0016940000000000002, -0.0037990000000000003, -0.006546, -0.011673000000000001, -0.001556, 0.022385, 0.049622, 0.084457, -0.10643, -0.10871900000000001, -0.10675, -0.09979199999999999, -0.094894],
        'RESP': [-1.148987, -1.124573, -1.152039, -1.158142, -1.161194, 2.2995, 2.305603, 2.297974, 2.354431, 2.355957, -1.222229, -1.202393, -1.222229, -1.228333, -1.210022, -0.883484, -0.880432, -0.8880620000000001, -0.8895870000000001, -0.865173],
        'LABEL': [0.0, 0.0, 0.0, 0.0, 0.0, 7.0, 7.0, 7.0, 7.0, 7.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
        'BINARY': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

df = pd.DataFrame(data)

# create separate dataframes with desired condition
mask = (df.BINARY == 1)
resp_1 = df[mask]
resp_others = df[~mask]

# plot the two dataframes
plt.figure(figsize=(12, 6))
plt.scatter(x=resp_1.index, y=resp_1.RESP, color='g', label='BINARY=1')
plt.scatter(x=resp_others.index, y=resp_others.RESP, label='BINARY!=1')
plt.legend()