Search code examples
pythonplotly

Python Plotly scatter 3D plot colormap customization


I am using plotly. I am getting the plot. The problem is, I am using seasons as colormap. I have used 1 for fall, 2 for winter, ..,4 for summer. Now, the colomap shows these numbers and also 1.5, 2.5 etc. I want to show Names instead of numbers

My code:

import plotly.express as px
from plotly.offline import plot
import plotly
fig = px.scatter_3d(df, x=xlbl, y=ylbl, z=zlbl,
              color=wlbl,opacity=0,
              color_continuous_scale  = plotly.colors.sequential.Viridis)
temp_name = 'Temp_plot.html'
plot(fig, filename = temp_name, auto_open=False,
     image_width=1200,image_height=800)    
plot(fig)

Present output:

enter image description here


Solution

  • You can modify the coloraxis by adding the following lines to your code:

    cat_labels = ["Fall", "Winter", "Spring", "Summer"]
    fig.update_coloraxes(colorbar=dict(ticktext=cat_labels, 
                                       tickvals=list(range(1, len(cat_labels)+1))))
    

    Sample output with random data: enter image description here