簡體   English   中英

在plotly-dash散點圖中設置長度來懸停文本

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

我在Dash中有一個散點圖,其中的text屬性(設置了懸停時顯示的文本)被設置為從數據框中的特定列獲取文本。

問題在於某些懸停文本太長而無法顯示在頁面上。 有沒有一種方法可以將懸停長度設置為固定長度,以免發生這種情況?

我已經看到使用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)

暫無
暫無

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

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