2017-08-08 104 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

相關問題