python - 如何基于两个因素定义散点图点颜色的条件语句?

标签 python conditional-statements conditional-formatting plotly-dash

本周我开始学习 Python 和 Dash,因为我渴望学习两者。

我的目标: 我想挑出传感器检测到的某些点,如果满足两个条件,则将它们标记为红色:

  • 如果在上午 01:00-05:00 之间检测到
  • 如果在某个区域检测到它们(假设我有一个列表,其中包含 每个区域的风险概率)。因此它还应该考虑任何大于 0.6 的风险。

着色的预期条件:

IF x > 1 AND x < 5 AND risk[i] > 0.6 THEN color=red, ELSE listCoords.index.hour

我的问题: 我的代码的 color=np.append(np.insert(listCoords.index.hour, 0, 0), 23) 部分必须如何更改才能实现我的目标?我在想一些类似的事情(尚未包括风险列表):

color=np.where(np.logical_and(listCoords.index.hour >= 1, listCoords.index.hour <= 5),
np.append(np.insert(listCoords.index.hour, 0, 0), 23), 'red')

但这不起作用。非常感谢任何帮助。

我的代码片段:地理 map 上的点散布:

return go.Figure(
    data=[
        # Data for all observations based on date/time
        Scattermapbox(
            lat=listCoords["Lat"],
            lon=listCoords["Lon"],
            mode="markers",
            hoverinfo="text + lat + lon",
            text=listCoords.index.hour,
            marker=dict(
                showscale=True,  
                color=np.append(np.insert(listCoords.index.hour, 0, 0), 23),
                opacity=0.5,
                size=5,
                colorscale=[
                    [0, "#F4EC15"],
                    [0.04167, "#DAF017"],
                    [0.0833, "#BBEC19"],
                    [0.125, "#9DE81B"],
                    [0.1667, "#80E41D"],
                    [0.2083, "#66E01F"],
                    [0.25, "#4CDC20"],
                    [0.292, "#34D822"],
                    [0.333, "#24D249"],
                    [0.375, "#25D042"],
                    [0.4167, "#26CC58"],
                    [0.4583, "#28C86D"],
                    [0.50, "#29C481"],
                    [0.54167, "#2AC093"],
                    [0.5833, "#2BBCA4"],
                    [1.0, "#613099"],
                ],
                colorbar=dict(
                    title="Time of<br>Day",
                    x=0.93,
                    xpad=0,
                    nticks=24,
                    tickfont=dict(color="#d8d8d8"),
                    titlefont=dict(color="#d8d8d8"),
                    thicknessmode="pixels",
                ),
            ),
        ),

编辑 1:提供示例数据以便人们可以运行它:

List of some sensor detections with their date/time stamp:

**Date/Time          Lat                 Lon**
2019-03-25 00:05:00 -10,80948998827914  24,19160777427344  
2019-03-25 00:10:00 -10,79868405083584  24,16288145431259
2019-03-25 04:05:00 -10,78688335083584  24,20288145431259
2019-03-25 04:05:00 -10,77558405083584  24,288145431259

List of zones and their risk probability:

ZoneRisk_Prob          Zone
0                       1
0                       2
0,002394936420140275    3
0,030364372469635626    4
0,00005702229571762559  5
0                       6
0,039345384045161656    7
0,10164224211666761     8
0,14854308034441466     9
0,0037064492216456633   10
0                       11
0                       12
0,0003421337743057536   13
0,1214289787306837      14
0,04410674573758339     15
0                       16
0                       17
0,0158236870616411      18
0                       19
0,18951359981752866     20
0,0014825796886582653   21
0,0005417118093174431   22
0,027769858014483662    23
0,014027484746535895    24
0,0012259793579289502   25
0,029737127216741745    26
0,009636767976278725    27
0,060072988538518564    28
0,043051833266807324    29
0,005759251867480185    30
0,1094257854821235      31

最佳答案

要获得满足您要求的职位,您应按以下步骤操作。

读取两个应该同步的数据文件,从某种意义上说 日期时间文件第 k 行中记录的日期时间,对应于另一个文件第 k 行中坐标(lon、lat)的位置。

dfz = pd.read_csv("zone-file.csv")
probs = dfz['ZoneRisk_Prob'].values
I = np.where(probs > 0.6)[0]
print(I)
dft = pd.read_csv("record-datetime.csv") #supose that this dataframe has Date/Time as index

#extract the list of datetimes between two fixed hours:

time_red = dft.between_time('01:00', '05:00').index
print(time_red)

#get the row number in dft.index of each element in  time_red

row_nr = [list(df.index).index(tr) for tr in time_red]
print(row_nr)

#extract the common elements in I and row_nr:

red_position = set(I).intersection(set(row_nr))

例如,如果 red_position =[ 7,12, 17, 25], 那么 color[7], color[12], color[17], color[25] 将为红色

关于python - 如何基于两个因素定义散点图点颜色的条件语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59783363/

相关文章:

python - 如何使用 `pip install git+ssh` 安装附加功能?

python - 从列表中生成多个条件语句

javascript - 基于条件语句调用具有多个参数的函数

r - 有条件地替换数据集 R 上的值

c# - 如何使用 Excel COM Interop 以编程方式对包含数字的单元格范围应用条件格式?

python - 未定义名称 'WorksheetNotFound'

Python:使用 SciPy/Pandas 的邻接矩阵的边列表显示 IndexError:列索引 (3) 超出范围

python - 将二维数组组合成元组

Excel,查看表中的值,其中行和列都是范围

python - 使用 Pandas 对重复项进行条件格式化