簡體   English   中英

Plotly:如何在帶注釋的熱圖中舍入顯示文本,但在 hover 上保持完整格式?

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

not_round

我想對浮點數進行四舍五入,因此它們在. 點。

當前的解決方法實際上是在輸入之前將 pandas dataframe 舍入。

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

圓形的

但是當我將鼠標懸停在 hover 上時,解決方法也會對文本進行四舍五入。 我想要 hover 文本的完整細節,而顯示文本是圓形的。

我可以在不更改輸入 dataframe 的情況下在每個單元格上顯示更少的數字嗎?

我只能假設您正在從列表列表中構建您的ff.create_annotated_heatmap() ,就像它們在 Python中的注釋熱圖下的文檔中所做的那樣。 如果您使用的是 pandas dataframe,請不要擔心。 下面的完整片段將向您展示如何從 pandas dataframe 與多個股票px.data.stocks的時間序列構建相關矩陣,然后使用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()那樣更改源 dataframe,(2) 在圖中僅顯示 1 個數字,並且 (3) 在 hover 上顯示更長的數字格式。 在圖像中,我懸停在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()

我手頭沒有數據,所以無法檢查執行情況,但我認為下面的代碼會起作用。 請參考官方參考

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM