【问题标题】:How to color markers based on another column in the dataframe in Plotly?如何根据 Plotly 中数据框中的另一列为标记着色?
【发布时间】:2020-04-21 16:31:31
【问题描述】:

我有一个如下所示的包含 3 列的数据框。我使用丛作为我的 x 值和统一大小作为我的 y 值来形成散点图。但我想根据第三列类为各个点着色。类值 2 为绿色和 4 为蓝色的点。

因此以数据框中的第一个和最后一个点为例。第一个点的 x 值为 5,y 值为 1,颜色为绿色,而最后一个点的 x 值为 4,y 值为 8,颜色为蓝色

我尝试使用如图所示的 if 语句,但出现语法错误。关于如何做到这一点的任何想法?

 fig = go.Figure()
 fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
 fig.add_trace(go.Scatter(x = data.Clump,
                          y = data.UnifSize,
                          mode = 'markers',
                          if data.Class == 2:
                              marker = duct(
                              color = 'green'
                              ) 
                          if data.Class == 4:
                             marker = dict(
                             color = 'yellow'
                             )
                     )))

【问题讨论】:

    标签: python python-3.x plot graph plotly


    【解决方案1】:

    你可以这样做:

    创建示例 xy 数据,其中包含颜色所依赖的条件的数组:

    import numpy as np
    x = [x for x in range(100)]
    y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
    condition = [np.random.randint(0,2) for x in range(100)]
    

    xy 点的索引对应于条件数组中的 0 是:

    [eachx for indexx, eachx in enumerate(x) if condition[indexx]==0]
    [eachy for indexy, eachy in enumerate(y) if condition[indexy]==0]
    

    如果我们希望 x 和 y 数组中的元素的索引对应于条件数组中的 1,我们只需将 0 更改为 1

    [eachx for indexx, eachx in enumerate(x) if condition[indexx]==1]
    [eachy for indexy, eachy in enumerate(y) if condition[indexy]==1]
    

    或者,您可以使用zip

    [eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]
    

    其他的以此类推。

    这是一个带有条件的列表推导,这里有很好的解释:https://stackoverflow.com/a/4260304/8565438

    然后绘制带有 2 个 go.Scatter 调用的 2 对数组。

    整件事在一起:

    import numpy as np
    x = [x for x in range(100)]
    y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
    condition = [np.random.randint(0,2) for x in range(100)]
    
    import plotly.graph_objects as go
    fig = go.Figure()
    fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
    fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==0],
                            y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==0],
                            mode = 'markers',marker = dict(color = 'green')))
    fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==1],
                            y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==1],
                            mode = 'markers',marker = dict(color = 'yellow')))
    fig.show()
    

    这会给你:

    我相信这正是我们想要的。


    要从DataFrame 列转换为list,建议使用:get list from pandas dataframe column

    【讨论】:

    • 我觉得[eachx for indexx, eachx in enumerate(x) if condition[indexx]==0]可以换成[eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]
    • 现在收录了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2017-07-13
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多