Plotly:如何在带注释的热图中舍入显示文本,但在悬停时保持完整格式? [英] Plotly: How to round display text in annotated heatmap but keep full format on hover?
问题描述
我正在绘制泰坦尼克号数据集的相关矩阵.
df_corr = df.corr()
最初,矩阵是这样的:
fig = ff.create_annotated_heatmap(z=df_corr.to_numpy(),x=df_corr.columns.tolist(),y=df_corr.index.tolist(),zmax=1, zmin=-1,显示比例=真,悬停=真)# 添加标题fig.update_layout(title_text='<i><b>Correlation not round</b></i>')
我想对浮点数进行四舍五入,因此它们在 .
点之后显示较少的数字.
当前的解决方法实际上是在输入之前围绕 pandas 数据框.
df_corr_round = df_corr.round(3)fig = ff.create_annotated_heatmap(z=df_corr_round.to_numpy(),x=df_corr.columns.tolist(),y=df_corr.index.tolist(),zmax=1, zmin=-1,显示比例=真,悬停=真)# 添加标题fig.update_layout(title_text='<i><b>相关轮</b></i>')
但是当我将鼠标悬停在上面时,解决方法也会使文本四舍五入.我想要完整详细的悬停文本,而显示文本是圆形的.
我可以在不更改输入数据框的情况下在每个单元格上显示更少的数字吗?
我只能假设您正在从列表列表中构建您的 ff.create_annotated_heatmap()
,就像他们在文档中所做的那样在
完整代码:
import plotly.express as px导入 plotly.figure_factory 作为 ff将熊猫导入为 pddf = px.data.stocks()#.tail(50)df = df.drop(['日期'], 轴 = 1)dfc = df.corr()z = dfc.values.tolist()# 将 z 的每个元素更改为用于注释的字符串类型# z_text = [[str(y) for y in x] for x in z]z_text = [[str(round(y, 1)) for y in x] for x in z]# 设置图形fig = ff.create_annotated_heatmap(z, x=list(df.columns),y=list(df.columns),annotation_text=z_text, colorscale='agsunset')# 添加标题fig.update_layout(title_text='<i><b>混淆矩阵</b></i>',#xaxis = dict(title='x'),#yaxis = dict(title='x'))# 添加自定义 xaxis 标题fig.add_annotation(dict(font=dict(color=black",size=14),x=0.5,y=-0.15,显示箭头=假,文字=",外部参照 =纸",yref="纸"))# 添加自定义 yaxis 标题fig.add_annotation(dict(font=dict(color=black",size=14),x=-0.35,y=0.5,显示箭头=假,文字=",文本角度=-90,外部参照 =纸",yref="纸"))# 调整边距为 yaxis 标题腾出空间fig.update_layout(margin=dict(t=50, l=200))# 添加颜色条fig['data'][0]['showscale'] = True图.show()
I am drawing a correlation matrix of the Titanic dataset.
df_corr = df.corr()
Originally, the matrix looks like this:
fig = ff.create_annotated_heatmap(
z=df_corr.to_numpy(),
x=df_corr.columns.tolist(),
y=df_corr.index.tolist(),
zmax=1, zmin=-1,
showscale=True,
hoverongaps=True
)
# add title
fig.update_layout(title_text='<i><b>Correlation not round</b></i>')
I want to round the float number, so they display less digits after the .
dot.
The current workaround is actually round the pandas dataframe before input.
df_corr_round = df_corr.round(3)
fig = ff.create_annotated_heatmap(
z=df_corr_round.to_numpy(),
x=df_corr.columns.tolist(),
y=df_corr.index.tolist(),
zmax=1, zmin=-1,
showscale=True,
hoverongaps=True
)
# add title
fig.update_layout(title_text='<i><b>Correlation round</b></i>')
But the workaround also rounds the text when I hover mouse over. I want hover text in full detail while display text are round.
Can I display less digits on each cell without changing the input dataframe ?
I can only assume that you're building your ff.create_annotated_heatmap()
from a list of lists as they do in the docs under Annotated Heatmaps in Python. And don't worry if you're using a pandas dataframe instead. The complete snippet below will show you how you construct a correlation matrix from a pandas dataframe with multiple timeseries of stocks px.data.stocks
, and then make a list of lists using df.values.tolist()
to build an annotated heatmap. If you're doing something similar, then one way of building the annotations would be to define a text like this:
z_text = [[str(y) for y in x] for x in z]
And then all you'll need to get the number of digits you want is use round():
z_text = [[str(round(y, 1)) for y in x] for x in z]
As you can see below, this approach (1) does not alter the source dataframe like df_corr.round()
would have, (2) shows only 1 digit in the figure, and (3) shows a longer number format on hover. In the image I'm hovering on MSFT / FB = 0.5
Complete code:
import plotly.express as px
import plotly.figure_factory as ff
import pandas as pd
df = px.data.stocks()#.tail(50)
df = df.drop(['date'], axis = 1)
dfc = df.corr()
z = dfc.values.tolist()
# change each element of z to type string for annotations
# z_text = [[str(y) for y in x] for x in z]
z_text = [[str(round(y, 1)) for y in x] for x in z]
# set up figure
fig = ff.create_annotated_heatmap(z, x=list(df.columns),
y=list(df.columns),
annotation_text=z_text, colorscale='agsunset')
# add title
fig.update_layout(title_text='<i><b>Confusion matrix</b></i>',
#xaxis = dict(title='x'),
#yaxis = dict(title='x')
)
# add custom xaxis title
fig.add_annotation(dict(font=dict(color="black",size=14),
x=0.5,
y=-0.15,
showarrow=False,
text="",
xref="paper",
yref="paper"))
# add custom yaxis title
fig.add_annotation(dict(font=dict(color="black",size=14),
x=-0.35,
y=0.5,
showarrow=False,
text="",
textangle=-90,
xref="paper",
yref="paper"))
# adjust margins to make room for yaxis title
fig.update_layout(margin=dict(t=50, l=200))
# add colorbar
fig['data'][0]['showscale'] = True
fig.show()
这篇关于Plotly:如何在带注释的热图中舍入显示文本,但在悬停时保持完整格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!