简体   繁体   English

用于情节表达热图的自定义悬停模板

[英]Custom hovertemplate for plotly express heatmap

I'm trying to use plotly.express in python to generate a heatmap from a pandas dataframe with a custom hover template that includes additional variables.我正在尝试在 python 中使用plotly.express从带有包含附加变量的自定义悬停模板的plotly.express数据帧生成热图 So for example:例如:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'A': [10, 11, 12], 'B': [10, 12, 14], 'C': [12, 14, 16]}, 
       index=['joe', 'tom', 'bob'])

fig = px.imshow(df)
fig.update_layout(xaxis={'title': 'Letters'}, yaxis={'title': 'Nicknames'})
fig.show()

Gives me this:给我这个:

热图

What I want is to add or replace a variable in the hover information, for instance, replace the nicknames with Joseph, Thomas and Robert.我想要的是添加或替换悬停信息中的变量,例如,将昵称替换为 Joseph、Thomas 和 Robert。

This has to be possible, but I can't figure out how to do it with a heatmap.这必须是可能的,但我无法弄清楚如何使用热图来做到这一点。 Is there a straight forward way to do this in plotly.express ?plotly.express是否有直接的方法来做到这plotly.express Should I use the "go" interface instead (if so, how)?我应该改用“go”界面(如果是,如何)?

I think I found the answer:我想我找到了答案:

import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame({'A': [10, 11, 12], 'B': [10, 12, 14], 'C': [12, 14, 16]}, 
       index=['joe', 'tom', 'bob'])

names = ['Joseph', 'Thomas', 'Robert']
fig = px.imshow(df)
fig.update(data=[{'customdata': np.repeat(names, len(df.columns)).reshape(3, 3),
    'hovertemplate': 'Letter: %{x}<br>Nickname: %{y}<br>Fullname: %{customdata}<br>Color: %{z}<extra></extra>'}])
fig.update_layout(xaxis={'title': 'Letters'}, yaxis={'title': 'Nicknames'})
fig.show()

在此处输入图片说明

Axis labels and scales can be customized in the following formats可以按以下格式自定义轴标签和比例

px.imshow(df, labels=dict(x='',y=''), x=[], y=[])

code:代码:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'A': [10, 11, 12], 'B': [10, 12, 14], 'C': [12, 14, 16]}, 
       index=['joe', 'tom', 'bob'])

fig = px.imshow(df, labels=dict(x='Lowcase Letters', y='Full Name'),
                x=['a','b','c'],
                y=['Joseph','Tomas','Robert'])

# fig.update_layout(xaxis={'title': 'Letters'}, yaxis={'title': 'Nicknames'})
fig.show()

在此处输入图片说明

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

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