Nicholas
Nicholas

Reputation: 3737

Plotly Heatmap - Python - Add colorbar

I have a heatmap that I have produced from a dataframe. Unfortunately it does not have a colorbar with it. I have tried adding one, but it doesnt show. Any ideas?

This is the code:

zp = df_hml.values.tolist()
zp.reverse()
z = zp
z_text = np.around(z) 

x = [threeYr,twoYr,oneYr,Yr]
y=['March', 'February', 'January', 'December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April']


layout = go.Layout(
    autosize=False,
    width=700,
    height=450,
    margin=go.Margin(
        l=150,
        r=160,
        b=50,
        t=100,
        pad=3
    ),
)
colorscale = [[0, '#454D59'],[0.5, '#FFFFFF'], [1, '#F1C40F']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text,colorscale=colorscale,  hoverinfo='z')

# Make text size smaller
for i in range(len(fig.layout.annotations)):
        fig.layout.annotations[i].font.size = 9

fig.layout.title = '<b>Research - HEATMAP</b><br>Applications'
fig.layout.titlefont.size = 14

fig.layout.width = 700
fig.layout.height = 550 
plotly.offline.iplot(fig, config={"displayModeBar": False}, show_link=False, filename='annotated_heatmap_numpy')

Which produces this:

enter image description here

Upvotes: 2

Views: 2602

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31739

The object returned by create_annotated_heatmap is virtually just a JSON. You can inspect all the parameters with print(fig). Just set fig['data'][0]['showscale'] = True and the scale bar will appear.

In addition you should set your colorscale to

colorscale = [[0, '#454D59'],
              [0.33, '#454D59'],
              [0.33, '#FFFFFF'],
              [0.66, '#FFFFFF'],
              [0.66, '#F1C40F'], 
              [1, '#F1C40F']]

i.e.

 [[lower bound, color1], [upper bound, color1],
  [lower bound, color2], [upper bound, color2],

the upper and lower bound of two different colors should be identical for discrete colorbars.

Upvotes: 4

Related Questions