首页 > 解决方案 > 在 Plotly 悬停文本(Python)中更改字体大小并限制文本长度

问题描述

我有一个由 9 个跟踪散点图组成的散点图,其中一个将进入 Dash Dashboard。

我只提供了第一个跟踪以使代码更短更容易,但是如果您需要查看其他 8 个,我可以提供它们(它们都与此跟踪相似)。

我需要帮助来执行以下操作:

  1. 更改悬停文本的字体大小

  2. 为悬停文本指定一个截止长度,就像它继续离开页面一样。

    trace0= go.Scatter(
        x =df[df['Topic'] == 'Time consuming tasks']['x'],
        y = df[df['Topic'] == 'Time consuming tasks']['y'],
        mode = 'markers',
        text= df[df['Topic'] == 'Time consuming tasks']['challenges'],
        marker = dict(      
            size = 9,
            line = dict(
                width = 2,
            )
        ),
    name = 'First Plot',
    showlegend=True)
    
    
    
    app.layout = html.Div([dcc.Graph(id='scatterplot',
                                            figure = {'data' : [trace0, trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8],
                                            'layout' : go.Layout(title='Biggest hindrances in your life?',
                                                                  xaxis = {'title':'x axis'},
                                                                  yaxis = {'title': 'y axis'},
                                                                 height = 550,
                                                                 titlefont= {'size':33},
                                                                 hovermode = 'closest',                                                                                                                                  
                                                                 legend=dict(
    
                                                                              traceorder='normal',
                                                                              font=dict(
                                                                                        family='sans-serif',
                                                                                        size=25,
                                                                                        color='#000'
                                                                                       )
                                                                             )
                                                                 )           
                                                    }                       
    
    
                                    )
                          ])
    

多谢你们!

标签: pythonplotlyplotly-dash

解决方案


我没有运行您的完整示例,但是,就我而言,以下解决了该问题:

go.Layout(title='Biggest hindrances in your life?',
          #other options for the plot
           hoverlabel=dict(font=dict(family='sans-serif', size=25)))

希望回答第1点。

编辑

刚刚找到这个页面,可能也有用 https://plotly.com/python/hover-text-and-formatting/


推荐阅读