简体   繁体   中英

Dash and Plotly plotting color markers doesn't appear to work and

This issue is bizzare. I cannot work out what is happening at all. I'm plotting a scatter chart with colors defined into two categories, when I see the chart, the colors do not correspond at all with the color selection. I have tried hard reload on chrome, but still the problem persists.

For example if I hover over the point that states "Green" it appears as "Red". I don't understand why this should be the case. The data is sensitive hence my reluctance to share the screen shots which are maddening!

When I look at the raw table that I generate it appears perfectly fine

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

here is the code for plotting

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

and the dash

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)

I think the issue here is I want to set the color for each of my points explicity as the color defined by the field set in the Column named "Color". Sadly it doesn't do this, hence the confusion, so a default is used. How can I set the color of a point explicitly for each and every row according to my data not letting plotly define the colors for me.

color in px.scatter() does not accept actual colors as arguments, but uses the input such as color=df_result["Color"] to assign a color sequence to unique values of df_result["Color"] in order to enhance the visual representation of attributes in your dataset:

color (str or int or Series or array-like) – Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign color to marks.

Take a look at Setting size and color with column names for some more details. You can also read through Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? for even more elaborate examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM