Plotly:如何在带注释的热图中舍入显示文本但在悬停时保持完整格式?

Posted

技术标签:

【中文标题】Plotly:如何在带注释的热图中舍入显示文本但在悬停时保持完整格式?【英文标题】:Plotly: How to round display text in annotated heatmap but keep full format on hover? 【发布时间】:2021-04-29 01:34:08 【问题描述】:

我正在绘制泰坦尼克号数据集的相关矩阵。

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,
            showscale=True,
            hoverongaps=True
            )
# add title
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,
            showscale=True,
            hoverongaps=True
            )
# add title
fig.update_layout(title_text='<i><b>Correlation round</b></i>')

但是当我将鼠标悬停在上面时,解决方法也会使文本四舍五入。我希望在显示文本是圆形的同时详细地悬停文本。

我可以在不更改输入数据框的情况下在每个单元格上显示更少的数字吗?

【问题讨论】:

【参考方案1】:

我只能假设您正在从列表列表中构建您的 ff.create_annotated_heatmap(),就像它们在 Annotated Heatmaps in Python 下的文档中所做的那样。如果您使用的是 pandas 数据框,请不要担心。下面的完整 sn-p 将向您展示如何从具有多个股票时间序列 px.data.stocks 的 pandas 数据框构建 correlation matrix,然后使用 df.values.tolist() 制作列表列表以构建带注释的热图。如果您正在做类似的事情,那么构建注释的一种方法是定义这样的文本:

z_text = [[str(y) for y in x] for x in z]

然后你需要得到你想要的位数就是使用round():

z_text = [[str(round(y, 1)) for y in x] for x in z]

正如您在下面看到的,这种方法 (1) 不会df_corr.round() 那样改变源数据帧,(2) 在图中仅显示 1 位数字,并且 (3) 显示悬停时更长的数字格式。在图片中我悬停在MSFT / FB = 0.5

完整代码:

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

【讨论】:

【参考方案2】:

我手头没有数据,所以无法检查执行情况,但我认为下面的代码会起作用。请参考official reference。

df_corr_round = df_corr.round(3)
fig = ff.create_annotated_heatmap(
            z=df_corr,
            x=df_corr.columns.tolist(),
            y=df_corr.index.tolist(),
            zmax=1, zmin=-1,
            showscale=True,
            hoverongaps=True,
            annotation_text=df_corr_round.to_numpy(),
            )
# add title
fig.update_layout(title_text='<i><b>Correlation round</b></i>')

【讨论】:

以上是关于Plotly:如何在带注释的热图中舍入显示文本但在悬停时保持完整格式?的主要内容,如果未能解决你的问题,请参考以下文章

Plotly:当 scaleanchor = x 时,如何调整带注释的热图的轴标签?

为啥用 Plotly 显示热图时会出现这个问题?

如何在 MySQL 中舍入 DateTime?

如何在动作脚本中舍入数字

如何在电报中舍入输出值?

如何在 Datagridview 中舍入 double 类型的值