gpt4 book ai didi

python - 通过在 Dash 中突出显示选择来放大两个图形

转载 作者:行者123 更新时间:2023-12-04 11:45:36 25 4
gpt4 key购买 nike

我试图使这两个图形具有响应性,以便如果我突出显示一个选择并放大其中一个,另一个图形会在相同的日期放大。

例如,在我发布的第一张图片中,如果我在一个图表上放大 1 月,我希望另一个图表也在相同的日期范围内放大。

你可以在我所附的图片中看到我想要做什么。

谁能帮帮我吗?

import dash
import dash_core_components as dcc
import dash_html_components as html
import ssl
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

ssl._create_default_https_context = ssl._create_unverified_context


df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

colors = {
'background': '#111111',
'text': '#7FDBFF'
}

app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
html.H1(
children='graph1',
style={
'textAlign': 'center',
'color': colors['text']
}
),

html.Div(children='Displaying Apple High and Low.', style={
'textAlign': 'center',
'color': colors['text']
}),

dcc.Graph(
id='example-graph-2',
figure={
'data': [
{'y': df['AAPL.Open'], 'x': df['Date'], 'type': 'line', 'name': 'Activity'},
{'y': df['AAPL.High'], 'x': df['Date'], 'type': 'line', 'name': 'Baseline'},
],
'layout': {
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
),

html.Div(children='Second Graph (Volume).', style={
'textAlign': 'center',
'color': colors['text']
}),

dcc.Graph(
id='graph2',
figure={
'data': [
{'y': df['AAPL.Volume'], 'x': df['Date'], 'type': 'bar', 'name': 'Volume'},
],
'layout': {
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
),


])



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

graph1

Graph 2

最佳答案

我对您的代码进行了以下更改:

  • 使用 plotly.graph_objects 从另一个图中配置由重新布局选择触发的回调中的两个图。在 graph1 上执行的每个重新布局都会触发 graph2 配置,反之亦然。
  • 重新布局数据来自 dict形式,我不得不将其 key 转换为 str访问其值。
  • 基于来自一个图形的重新布局数据,使用 update_xaxes 正确配置另一个图形的 x 轴范围.

  • 现在,基于一个图表中的 x 轴范围选择,另一个图表会自动更新。我在 graph2 下方添加了一个 Markdown 项目,以便您可以跟踪重新布局数据字典及其值。

    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly.graph_objects as go
    import ssl
    import pandas as pd
    from dash.dependencies import Input, Output

    external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

    ssl._create_default_https_context = ssl._create_unverified_context
    colors = {'background': '#111111',
    'text': '#7FDBFF'}

    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")

    app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

    #Sets df1 for graph1
    df1 = df[['Date', 'AAPL.Open', 'AAPL.High']]
    df1.columns = ['Date', 'Activity', 'Baseline']

    #Sets df2 for graph2
    df2 = df[['Date', 'AAPL.Volume']]
    df2.columns = ['Date', 'Volume']

    #App layout
    app.layout = html.Div(style={'backgroundColor': colors['background']},
    children=[

    html.H1(children='graph1',
    style={'textAlign': 'center',
    'color': colors['text']}),

    html.Div(children='Displaying Apple High and Low.',
    style={'textAlign': 'center',
    'color': colors['text']}),

    dcc.Graph(id='graph1'),

    html.Div(children='Second Graph (Volume).',
    style={'textAlign': 'center',
    'color': colors['text']}),
    dcc.Graph(id='graph2'),

    #item to track x axis relayout data
    dcc.Markdown(id= 'axis_data'),

    ])

    @app.callback(
    Output('graph2', 'figure'),
    Input('graph1', 'relayoutData'))

    def relayout_graph2(xaxis_config):

    figure2 = go.Figure()
    figure2.add_trace(go.Scatter(x = df2.Date, y = df2.Volume, name = 'Volume'))
    figure2.update_layout(plot_bgcolor = colors['background'],
    paper_bgcolor = colors['background'],
    font_color = colors['text'],
    showlegend = True)

    if not xaxis_config:
    pass

    else:

    #For some reason it's necessary to convert the relayout dict keys to strings
    keys_values = xaxis_config.items()
    xaxis = {str(key): value for key, value in keys_values}

    try:
    if 'autosize' in xaxis.keys():
    pass
    elif 'xaxis.autorange' in xaxis.keys():
    figure2.update_xaxes(autorange = True)
    else:
    figure2.update_xaxes(range=[xaxis['xaxis.range[0]'],xaxis['xaxis.range[1]']])

    except:
    pass

    return figure2

    @app.callback(
    Output('graph1', 'figure'),
    Input('graph2', 'relayoutData'))

    def relayout_graph1(xaxis_config):

    figure1 = go.Figure()
    figure1.add_trace(go.Scatter(x = df1.Date, y = df1.Activity, name = 'Activity'))
    figure1.add_trace(go.Scatter(x = df1.Date, y = df1.Baseline, name = 'Baseline'))
    figure1.update_layout(plot_bgcolor = colors['background'],
    paper_bgcolor = colors['background'],
    font_color = colors['text'])

    if not xaxis_config:
    pass

    else:

    #For some reason it's necessary to convert the relayout dict keys to strings
    keys_values = xaxis_config.items()
    xaxis = {str(key): value for key, value in keys_values}

    try:
    if 'autosize' in xaxis.keys():
    pass
    elif 'xaxis.autorange' in xaxis.keys():
    figure1.update_xaxes(autorange = True)
    else:
    figure1.update_xaxes(range=[xaxis['xaxis.range[0]'],xaxis['xaxis.range[1]']])

    except:
    pass

    return figure1

    @app.callback(
    Output('axis_data', 'children'),
    [Input('graph1', 'relayoutData'),
    Input('graph2', 'relayoutData')])

    def read_relayout(data1, data2):

    return str(data1) + '///' + str(data2)

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

    关于python - 通过在 Dash 中突出显示选择来放大两个图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606410/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com