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

标签 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)

最佳答案

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

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

关于Python Dash 绘图 : Show default closest data on hover or compare data on hover in graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54107595/

相关文章:

python - Miniconda 与 Python 3.6 使用哪个安装程序?

python - 背景图像来自 Google Drive 中的 plotly

python - Plotly Python 中的趋势线

python - 如何用plotly绘制椭圆体

python - 如何在 plotly 中使用聚合绘制箱线图?

python - 如何将 update_layout 边距限制为 Plotly/Dash 中的一个子图?

Python列表错误,列表索引超出范围

python - 我如何在 python 中连接到 Microsoft sql server 2008

python - 在python中从sql中提取数据(每秒一次)

python - Django 'global name' 错误