Python Dash Plotly:在鼠标悬停时显示默认的最近数据,或在图形中比较悬停时的数据

时间:2019-01-09 10:07:22

标签: python plotly plotly-dash

我正在使用Dash Plotly构建一个简单的应用程序。
默认设置是图形“比较悬停时的数据”。
enter image description here

我想将默认设置更改为“在悬停时显示最近的数据”: enter image description here

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

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)

2 个答案:

答案 0 :(得分:1)

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

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',
}

答案 1 :(得分:0)

至少在破折号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