我在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)