我正在使用 Dash Plotly 构建一个简单的应用程序。
默认设置是图表“比较悬停时的数据”。
我想将默认设置更改为“悬停时显示最接近的数据”:
如何在下面的代码中完成此操作?
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/