0

I have a heatmap that I created with this code:

dfreverse = df_hml.values.tolist()
dfreverse.reverse()

colorscale = [[0, '#FFFFFF'],[0.4, '#F8F8FF'], [1, '#F1C40F']]

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

z_text = np.around(z, decimals=2) # Only show rounded value (full value on hover)

fig = ff.create_annotated_heatmap(z, x=x, y=y,annotation_text=z_text, colorscale=colorscale, hoverinfo='none')


# Altering x axis
fig['layout']['xaxis']['tickfont']['family'] = 'Gill Sans MT'
fig['layout']['xaxis']['tickfont']['size'] = 12
fig['layout']['xaxis']['tickfont']['color'] = "black"
fig['layout']['xaxis']['tickangle'] = 0

# Altering x axis
fig['layout']['yaxis']['tickfont']['family'] = "Gill Sans MT"
fig['layout']['yaxis']['tickfont']['size'] = 12
fig['layout']['yaxis']['tickfont']['color'] = "black"
fig['layout']['yaxis']['tickangle'] = 25

# Altering main font
fig['layout']['font'] ["family"] = "Gill Sans MT"
fig['layout']['font']['size'] = 9

plotly.offline.iplot(fig,config={"displayModeBar": False},show_link=False,filename='pandas-heatmap')

enter image description here

As you can see, August to March this year doesnt have any data and thus is showing as 0. I cant remove this otherwise the heatmap doesnt work... so I was thinking I would change the font of any 0's to white to hide them. However I am not sure how to do this.

I found this code which changes the fontsize of the annotation, but not sure how to alter it to change the color if the value == 0 (as 'i' is not the value)

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

1 Answer 1

1

Instead of changing the annotation just loop through the z values and replace the 0 values with "". This is producing the same output. I have used some mockup data for simulating the issue.

Code:

import pandas as pd
import numpy as np
import plotly.figure_factory as ff
import plotly.offline as py_offline
py_offline.offline.init_notebook_mode()
colorscale = [[0, '#FFFFFF'],[0.4, '#F8F8FF'], [1, '#F1C40F']]

x = ['2015','2016','2017','2018']
y = ['March', 'February', 'January','April']
z = [[1,2.129,3,4],
    [0,0,1,2],
    [6,0,1,0],
    [6,0,0,2]]

z_text = []

for q, arr in enumerate(np.around(z, decimals=2)):
    z_text.append([str(h) if h else "" for h in arr])

fig = ff.create_annotated_heatmap(z, x=x, y=y,annotation_text=z_text, colorscale=colorscale, hoverinfo='none')


# Altering x axis
fig['layout']['xaxis']['tickfont']['family'] = 'Gill Sans MT'
fig['layout']['xaxis']['tickfont']['size'] = 12
fig['layout']['xaxis']['tickfont']['color'] = "black"
fig['layout']['xaxis']['tickangle'] = 0

# Altering x axis
fig['layout']['yaxis']['tickfont']['family'] = "Gill Sans MT"
fig['layout']['yaxis']['tickfont']['size'] = 12
fig['layout']['yaxis']['tickfont']['color'] = "black"
fig['layout']['yaxis']['tickangle'] = 25

# Altering main font
fig['layout']['font'] ["family"] = "Gill Sans MT"
fig['layout']['font']['size'] = 9

py_offline.iplot(fig,config={"displayModeBar": False},show_link=False,filename='pandas-heatmap')

Output: plotly annotations hide zero values

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Naren again :)... I will test this tomorrow morning as I am not at work, but your solution makes a lot more sense than what I was trying to do!
@ScoutEU no problem take your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.