python3dplotlytitlesurface

How to add a colorbar title to a 3D surface image when using Plotly?


I have been trying to create a 3D surface plot using Plotly with Python3. I am drawing based on the official case, but the case does not explain how to add a title to the colorbar.

I tried running the following command with no success.

fig.update_layout(colorbar=dict(title='Temperature(K)'), titleside='right')

The error display is: Bad property path: colorbar ^^^^^^^^ Also, is there a way to add units to the values on the color bar?


Solution

  • You can specify it in the go.Surface() constructor directly, eg. :

    fig = go.Figure(data=[go.Surface(z=zdata,
                                     colorbar_title_text='Colorbar Title')])
    

    Nb. colorbar_title_text uses "magic underscore" notation as a shortcut for colorbar=dict(title=dict(text='...')).

    See https://plotly.com/python/reference/surface/#surface-colorbar-title-text