Python Plotly - 在热图中更改注释上的字体颜色

时间:2017-08-16 12:27:28

标签: python plotly

我有一个使用此代码创建的热图:

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

正如你所看到的,今年8月到3月没有任何数据,因此显示为0.我不能删除这个,否则热图不起作用...所以我想我会将任何0的字体改为白色隐藏它们。但是我不知道该怎么做。

我发现这段代码改变了注释的字体大小,但是如果值== 0(因为'i'不是值),我不知道如何改变它来改变颜色

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

1 个答案:

答案 0 :(得分:1)

而不是更改注释只是循环遍历z值并用“”替换0值。这产生了相同的输出。我使用了一些模型数据来模拟问题。

<强>代码:

value

<强>输出: plotly annotations hide zero values