简体   繁体   English

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

[英]Python Plotly - Changing color of font on annotation in heatmap

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')

在此处输入图片说明

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. 如您所见,今年8月至3月没有任何数据,因此显示为0。我无法删除此数据,否则热图将不起作用...所以我想将所有0的字体更改为白色以隐藏它们。 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) 我发现此代码会更改注释的字体大小,但是不确定如果值== 0(因为'i'不是值)如何更改它以更改颜色

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

Instead of changing the annotation just loop through the z values and replace the 0 values with "". 无需更改注释,只需遍历z值并将0值替换为“”即可。 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: 输出: 绘图注解隐藏零值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM