Python Dash Plotly:在悬停时显示默认最接近的数据或在图表中比较悬停时的数据

Python Dash Plotly: Show default closest data on hover or compare data on hover in graph

我正在使用 Dash Plotly 构建一个简单的应用程序。
默认设置为图'Compares data on hover'。


我想将默认设置更改为 'Show closest data on hover':

如何在下面的代码中完成此操作?

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div(children=[
    html.H1(children='Hello New Other Change', ),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization',
            }
        }
    )
])

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

将图形默认设置为显示最接近的数据 可以通过向图形添加悬停模式来完成,如下所示:

figure={
    'data': [
        {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
        {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
    ],
    'layout': {
        'hovermode': 'closest',
    }
}

将图表默认设置为比较数据 由以下人员完成:

'layout': {
    'hovermode': 'compare',
}

更新 Sander van den Oord 的回答,至少从 dash 1.0.2 开始:

layout.hovermode = 'closest' # for "Show closest data on hover"
layout.hovermode = 'x' # for "Compare data on hover"

其他选项包括:

layout.hovermode = 'y' # similar to x but switches tags accordingly
layout.hovermode = False # nothing shown on hover