在 plotly-Dash 散点图中设置悬停文本的长度

Setting a length to hover text in plotly-Dash Scatter plot

我在 Dash 中有一个散点图,其中文本 属性(设置悬停时显示的文本)设置为从数据框中的特定列中获取文本。

问题是一些悬停文本太长而超出了页面。有没有办法给悬停长度一个固定的长度,这样就不会发生这种情况?

我已经看到使用 hoverformat 来处理数值数据。但是我的悬停信息是文字。

我不太确定是否有属性可以为 hoverinfo 设置固定大小,但是您始终可以在显示文本列表之前对文本列表进行一些预处理,这样更容易,也可以根据需要自定义.

这是一种方法,

import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import json
import pandas as pd

app = dash.Dash()


#Consider this as the dataframe to be shown in hover
L = ["Text A", "Text B", "Text C", "Text D", "Text E"]
df = pd.DataFrame({'col':L})


# Here write your custom preprocessing function, 
# We can do whatever we want here to truncate the list
# Here every element in the list is truncated to have only 4 characters
def process_list(a):
    return [elem[:4] for elem in a]

app.layout = html.Div([
    dcc.Graph(
        id='life-exp-vs-gdp',
        figure={
            'data': [
                go.Scatter(
                    x = [1,2,3,4,5],
                    y = [2,1,6,4,4],
                    #call the pre processing function with the data frame
                    text = process_list(df['col']),
                    hoverinfo = 'text',
                    marker = dict(
                        color = 'green'
                    ),
                    showlegend = False
                )
            ],
            'layout': go.Layout(
            )
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)