use*_*222 1 python plotly plotly-dash
我在 Dash 中有一个散点图,其中文本属性(设置悬停时显示的文本)设置为从数据框中的特定列获取文本。
问题是一些悬停文本太长并且超出了页面。有没有办法让悬停长度固定,这样就不会发生这种情况?
我已经看到使用悬停格式来处理数值数据。但我的悬停信息是文本。
我不太确定是否有一个属性可以为悬停信息设置固定大小,但您始终可以在显示文本列表之前对其进行一些预处理,这更容易,并且还可以根据需要进行自定义。
这是执行此操作的一种方法,
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)
Run Code Online (Sandbox Code Playgroud)