简体   繁体   中英

Plotly Python - Heatmap - Change Hovertext (x,y,z)

I have a heatmap done with plotly in python. The hovertext works perfectly, however it has each variable prefixed with x, y or z like this:

在此输入图像描述

It there any way to change this ie x = "FY", y = "Month" and z = "Count"

This is the code that produced the heatmap above

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

Thank you!

You can customize the hoverinfo by setting it to text and then enter whatever you like.

The text for the hoverinfo needs to be a list of lists. The first index is the y-value, the 2nd is the x-value.

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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