22

This is my code:

fig = go.Figure(
    data=go.Heatmap(z=z_values, y=[str(x) for x in params_1], x=[str(x) for x in params_2]), 
    layout=go.Layout(
        title="Analysis results",
        xaxis=dict(title='Diameter'),
        yaxis=dict(title='Max Distance')
    ),
)
fig.show()

It generates a 2D-heatmap (snippet below), but I'd like to include a title for the colorbar:

enter image description here

Unfortunately the plotly example also does not have a colorbar title. I have tried to include the colorbar properties with the "marker" but this throws an error. How can I do that hence?

vestland
  • 55,229
  • 37
  • 187
  • 305
TestGuest
  • 593
  • 1
  • 4
  • 16

5 Answers5

15

Just include colorbar={"title": 'Your title'} in go.Heatmap() to get this:

Plot:

enter image description here

Code:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(colorbar={"title": "Your title"},
                                            z=[[1, 20, 30],
                                              [20, 1, 60],
                                              [30, 60, 1]]))
 fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
13

Try

fig = go.Figure(
    data=go.Heatmap(z=z_values, y=[str(x) for x in params_1], x=[str(x) for x in params_2]), 
colorbar=dict(title='Title') , 
    layout=go.Layout(
        title="Analysis results",
        xaxis=dict(title='Diameter'),
        yaxis=dict(title='Max Distance')
    ),
)
fig.show()
abhilb
  • 5,639
  • 2
  • 20
  • 26
8

You can also do it after by updating the layout:

fig.update_layout(
    coloraxis_colorbar=dict(
        title="Your Title",
    ),
)
Piotro
  • 525
  • 5
  • 5
  • did NOT work for me... plotly==5.9.0 – Julian - BrainAnnex.org Aug 28 '22 at 00:33
  • 1
    @Julian-BrainAnnex.org -- I had a similar issue, but found that this works if you specify the coloraxis the heatmap should use. E.g. ``` fig = go.Figure() fig.add_trace(go.Heatmap(x=..., y=..., z=..., coloraxis='coloraxis')) fig.update_layout(coloraxis_colorbar_title='Your Title') ``` – Tim Child Sep 22 '22 at 17:45
3
fig = ...
fig.layout.coloraxis.colorbar.title = 'Title<br>Here'
Miladiouss
  • 4,270
  • 1
  • 27
  • 34
  • 2
    This one is universal and works with px.imshow as well. You can edit the title more using standard plotly arguments here as well: `{'text':'Title', 'font':{'size':30}}` – lotrus28 Aug 23 '21 at 20:07
  • 1
    This works with all plotly express plots as well. Very helpful! – codeananda Apr 14 '22 at 06:07
  • did NOT work for me... plotly==5.9.0 – Julian - BrainAnnex.org Aug 28 '22 at 00:34
  • @Julian-BrainAnnex.org -- I had a similar issue, but found that this works if you specify the coloraxis the heatmap should use. E.g. ``` fig = go.Figure() fig.add_trace(go.Heatmap(x=..., y=..., z=..., coloraxis='coloraxis')) fig.update_layout(coloraxis_colorbar_title='Your Title') ``` – Tim Child Sep 22 '22 at 17:45
1

None of these were working for me but doing this updated it without any issue:

fig = ...
fig.data[0].colorbar.title = "Title Here"
DaPieDude
  • 31
  • 6
  • 1
    Works perfectly in plotly==5.9.0 If you do a `print(fig)` , you can see that the Figure object now contains `'data': [{'colorbar': {'title': {'text': 'Title Here'}},` etc Note that with this approach, we're back to adding the colorbar title in the *data* section, just as done in the chosen answer – Julian - BrainAnnex.org Aug 28 '22 at 00:38