【问题标题】:How to solve Invalid element(s) received for the 'color' property?如何解决“颜色”属性收到的无效元素?
【发布时间】:2020-01-20 18:43:37
【问题描述】:

我有一段代码,可以根据观察到的时间为散点图点提供各种颜色。这是按照以下代码完成的:

color=listCoords.index.hour

在上下文代码中:

return go.Figure(
        data=[
            # Data for all observations based on date and time
            Scattermapbox(
                lat=listCoords["Lat"],
                lon=listCoords["Lon"],
                mode="markers",
                hoverinfo="text + lat + lon",
                text=listCoords.index.hour,
                marker=dict(
                    showscale=True,
                    color=listCoords.index.hour,
                    opacity=np.where((listCoords['Threat'] == '3'), 0.1, 0.7), 
                    size=np.where((listCoords['Threat'] == '3'), 20, 7),
                    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",
                    ),
                ),
            ),

我想把'color='改成:

color=np.where((listCoords['Threat'] == '3'), 'red', listCoords.index.hour)

如果点的“威胁”值等于“3”,则这些点变为“红色” 否则“颜色”应该只是 'listCoords.index.hour'

但是这个新的代码更新给出了:

ValueError: 
    Invalid element(s) received for the 'color' property of scattermapbox.marker
        Invalid elements include: ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']

    The 'color' property is a color and may be specified as:
      - A hex string (e.g. '#ff0000')
      - An rgb/rgba string (e.g. 'rgb(255,0,0)')
      - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
      - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
      - A named CSS color:
            aliceblue, antiquewhite, aqua, aquamarine, azure,
            beige, bisque, black, blanchedalmond, blue,
            blueviolet, brown, burlywood, cadetblue,
            chartreuse, chocolate, coral, cornflowerblue....

问题:所以是的,它显然需要 CSS 颜色。但是看到最初的'color='确实认为listCoords.index.hour 一个值,如果不是,我怎样才能让这个项目再次考虑该值 “红色”?

【问题讨论】:

    标签: python css plotly-dash


    【解决方案1】:

    解决方案:

    应用以下部分:

    color=np.where((listCoords['Threat'] == '3'), 'red', listCoords.index.hour)
    

    它输出了错误:

    ValueError: 
        Invalid element(s) received for the 'color' property of scattermapbox.marker
            Invalid elements include: ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']
    
        The 'color' property is a color and may be specified as:
          - A hex string (e.g. '#ff0000')
          - An rgb/rgba string (e.g. 'rgb(255,0,0)')
          - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
          - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
          - A named CSS color:
                aliceblue, antiquewhite, aqua, aquamarine, azure,
                beige, bisque, black, blanchedalmond, blue,
                blueviolet, brown, burlywood, cadetblue,
                chartreuse, chocolate, coral, cornflowerblue....
    

    为了解决这个问题,我通过说 0 = 'red' (#FF0000) 来更改色阶。

    随后,我说 if ('Threat == 3' give 0) else 给出常规颜色。

    我的新代码(仅限已更新的部分):

        red = 0
        mycolors = np.where((listCoords['Threat'] == '3'), red, listCoords.index.hour)
        print(mycolors)
    

      color=np.array(mycolors, int),
                    colorscale=[
                        [0, "#FF0000"],
                        [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"],
                    ],
    

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 2023-01-24
      • 2018-09-28
      • 1970-01-01
      • 2021-08-07
      • 2016-09-29
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多