【问题标题】:Dash and Plotly plotting color markers doesn't appear to work andDash 和 Plotly 绘制颜色标记似乎不起作用,并且
【发布时间】:2022-01-06 12:26:10
【问题描述】:

这个问题很奇怪。我根本无法弄清楚发生了什么。我正在绘制一个散点图,其中将颜色定义为两类,当我看到图表时,颜色与颜色选择完全不对应。我已经尝试在 chrome 上重新加载,但问题仍然存在。

例如,如果我将鼠标悬停在显示“绿色”的点上,它会显示为“红色”。我不明白为什么会这样。数据很敏感,因此我不愿分享令人抓狂的屏幕截图!

当我查看我生成的原始表时,它看起来非常好

df_result["Color"] = np.where((df_result["Lift"] < 0), 'red', 'green')

这是绘图的代码

fig2d = px.scatter(df_result, x="Lift", y="Lift2", hover_data=['Line'], size=df_result["S"].abs(), color=df_result["Color"])

还有破折号

app.layout = html.Div(children=[
    html.H1(children=''),

    html.Div(children='''
        Exploring data.
    '''),

    dcc.Graph(
        id='j-2d',
        figure=fig2d
    ),

])

if __name__ == '__main__':
    app.run_server(debug=True)

我认为这里的问题是我想将每个点的颜色明确设置为由名为“颜色”的列中设置的字段定义的颜色。遗憾的是它没有这样做,因此造成混乱,因此使用了默认值。如何根据我的数据为每一行明确设置一个点的颜色,而不是让我为我定义颜色。

【问题讨论】:

    标签: python plotly plotly-dash


    【解决方案1】:

    px.scatter() 中的color 不接受实际颜色作为参数,而是使用诸如color=df_result["Color"] 之类的输入将颜色序列分配给df_result["Color"] 的唯一值,以增强您的属性的视觉表示数据集:

    color (str or int or Series or array-like) – 列名 在 data_frame 或 pandas Series 或 array_like 对象中。值来自 此列或 array_like 用于为标记分配颜色。

    查看Setting size and color with column names 了解更多详情。您还可以阅读Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? 了解更详细的示例。

    【讨论】:

    • 是的,应该是传入的内容,df_result 中名为“Color”的列,其每个值都有一个颜色“红色”或“绿色”。我以前做过这个,过去从来没有遇到过任何问题。 “颜色”列对应于每一行的“红色”或“绿色”值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多