Search code examples
pythoncolorsplotlyscatter-plot

Plotly: n qualitative colors


I am using Plotly and Python 3 for data visualization. It is known to me that default number of colors in Plotly is 10 and are repeated in cycle if more elements than 10.

I do not want to use this approach to bring more colors in my 3D scattegram: How to create discrete colormap with n colors using Plotly cause the colors look too similar.

I am looking for a solution that would assign one unique qualitative color to each element (cluster) in my code. Here is my code:

fig_tSNE = px.scatter_3d(x = projections[:,0],
                      y = projections[:,1],
                      z = projections[:,2],                  
                      color = to_be_plotted['Cluster'],
                      color_discrete_sequence = px.colors.sequential.PuBu)

Thanks for help.


Solution

  • As Alec said in the comment, you can build your color palette. Alternatively, you can use one of the available discrete color maps.

    import plotly.express as px
    import numpy as np
    
    n = 100
    ncat = 20
    xx = np.random.uniform(-10, 10, n)
    yy = np.random.uniform(-10, 10, n)
    zz = np.random.uniform(-10, 10, n)
    cc = np.random.uniform(0, ncat, n).astype(int).astype(str)
    px.scatter_3d(x=xx, y=yy, z=zz, color=cc, color_discrete_sequence=px.colors.qualitative.Alphabet)
    

    enter image description here