2017-08-08 162 views
1

我在python中绘制了一个热图。该hovertext完美的作品,但它的每个变量前缀X,Y或Z这样的:Plotly Python - Heatmap - 改变Hovertext(x,y,z)

enter image description here

它有什么办法来改变这种即X =“风云”,Y =“月”和z = “计数”

这是产生上述

热图码
dfreverse = df_hml.values.tolist() 
dfreverse.reverse() 

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

trace = go.Heatmap(z=dfreverse, 
        colorscale = colorscale, 
        x = [threeYr,twoYr,oneYr,Yr], 
        y=['March', 'February', 'January', 'December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April']) 
data=[trace] 



layout = go.Layout(
    autosize=False, 
    font=Font(
     family="Courier New", 
    ), 
    width=700, 
    height=450, 
    margin=go.Margin(
     l=150, 
     r=160, 
     b=50, 
     t=100, 
     pad=3 
    ), 
) 

fig = go.Figure(data=data, layout=layout) 
plotly.offline.iplot(fig,filename='pandas-heatmap') 

谢谢!

回答

2

您可以通过将其设置为text来自定义hoverinfo,然后输入您喜欢的任何内容。

hoverinfo的文本需要是列表的列表。第一个索引是y值,第二个索引是x值。

enter image description here

import plotly 
import random 
plotly.offline.init_notebook_mode() 

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

x = [2015, 2016, 2017] 
y = ['March', 'February', 'January', 'December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April'] 
z = [[i + random.random() for i in range(len(x))] for ii in range(len(y))] 

hovertext = list() 
for yi, yy in enumerate(y): 
    hovertext.append(list()) 
    for xi, xx in enumerate(x): 
     hovertext[-1].append('FY: {}<br />Month: {}<br />Count: {}'.format(xx, yy, z[yi][xi])) 

data = [plotly.graph_objs.Heatmap(z=z, 
            colorscale=colorscale, 
            x=x, 
            y=y, 
            hoverinfo='text', 
            text=hovertext)] 

layout = plotly.graph_objs.Layout(autosize=False, 
            font=dict(family="Courier New"), 
            width=700, 
            height=450, 
            margin=plotly.graph_objs.Margin(l=150, 
                    r=160, 
                    b=50, 
                    t=100, 
                    pad=3) 
           ) 

fig = plotly.graph_objs.Figure(data=data, layout=layout) 
plotly.offline.iplot(fig,filename='pandas-heatmap') 
+0

太感谢你了。我对为达到这一目标所花费的时间表示歉意,在做出回应之前我完成了工作。我只记得登录并检查:)....它完美的工作! – ScoutEU

+0

编辑:我真的很感谢你接受你的详细回复! – ScoutEU

相关问题