Search code examples
pythonplotlydata-visualizationscatter-plot

Change Plotly scatter plot color


I am trying to visualize a 4-class dataset via plotly.

For the following code : fig = px.scatter_3d(df_pca, x='pca1', y='pca2', z='pca3', color = 'emotion')

I get the below graph which is not convenient to understand because the "blue" and "purple" colors are "indiscernible".

So I would like to change the "purple" color to "yellow". Could you please advise how to do it in plotly? The documentation does not seem very helpful.

enter image description here


Solution

  • I found this color_discrete_sequence parameter in the documentation, perhaps it can help you https://plotly.com/python/discrete-color/#explicitly-constructing-a-color-sequence

    import plotly.express as px
    df = px.data.gapminder().query("year == 2007")
    fig = px.bar(df, y="continent", x="pop", color="continent", orientation="h", hover_name="country",
                 color_discrete_sequence=["red", "green", "blue", "goldenrod", "magenta"],
                 title="Explicit color sequence"
                )
    
    fig.show()
    

    Bar plot with discrete color sequence