我正在尝试在python中使用plotly.express
从带有自定义悬停模板(包括其他变量)的熊猫数据框中生成heatmap。例如:
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()
给我这个:
我想要的是在悬停信息中添加或替换变量,例如,用Joseph,Thomas和Robert替换昵称。
这必须是可能的,但是我不知道如何通过热图来实现。在plotly.express
中有直接的方法吗?我应该改用“ go”界面(如果可以的话,如何使用)?
答案 0 :(得分:1)
可以使用以下格式自定义轴标签和刻度
px.imshow(df, labels=dict(x='',y=''), x=[], y=[])
代码:
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()
答案 1 :(得分:1)
我想我找到了答案:
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()