冻结点状图可视化

问题描述 投票:0回答:1

我正在使用破折号应用程序,这是我的代码:

import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go

blue = '#6683f3'
orange = '#ff9266'
grey = '#e0e1f5'
black = '#212121'

app = dash.Dash()

app.layout = html.Div([html.Div([dcc.RadioItems(id = 'radio-item',
                                                options = [dict(label = 'squared',
                                                                value = '2'),
                                                           dict(label = 'cubed',
                                                                value = '3')],
                                                value = '2',
                                                labelStyle = {'display': 'block'})]),

                       html.Div(dcc.Graph(id = 'graph',
                                          figure = dict(data = [],
                                                        layout = go.Layout(plot_bgcolor = black)),
                                          style = dict(height = 1000)))])


@app.callback(Output('graph', 'figure'),
              [Input('radio-item', 'value')])
def update_graph(value):

    x = np.linspace(0, 10, 100)
    y = np.linspace(0, 10, 100)
    XX, YY = np.meshgrid(x, y)

    Z1 = XX ** int(value) * YY
    Z2 = XX ** int(value) / YY

    data = [go.Contour(z = Z1,
                       name = f'Z1',
                       zmin = 0,
                       zmax = 100,
                       contours_coloring = 'lines',
                       line_width = 2,
                       showscale = False,
                       showlegend = True,
                       visible = True,
                       colorscale = [[0, orange], [1, orange]],
                       ncontours = 21,
                       contours = dict(showlabels = True,
                                       labelformat = '.0f')),

            go.Contour(z = Z2,
                       name = f'Z2',
                       zmin = 0,
                       zmax = 100,
                       contours_coloring = 'lines',
                       line_width = 2,
                       showscale = False,
                       showlegend = True,
                       visible = True,
                       colorscale = [[0, blue], [1, blue]],
                       ncontours = 21,
                       contours = dict(showlabels = True,
                                       labelformat = '.0f'))]

    layout = go.Layout(plot_bgcolor = black,
                       hovermode = 'x unified')

    figure = go.Figure(data = data, layout = layout)

    figure.update_xaxes(title_text = 'X',
                        linewidth = 1,
                        nticks = 11,
                        gridwidth = 0.5,
                        gridcolor = grey,
                        tickformat = '.0f')

    figure.update_yaxes(title_text = 'Y',
                        linewidth = 1,
                        nticks = 11,
                        gridwidth = 0.5,
                        gridcolor = grey,
                        tickformat = '.0f')

    figure.update_layout(legend = dict(itemsizing = 'constant'))

    return figure


if __name__ == "__main__":
    app.run_server()

此代码生成图形和带有两个选项的RadioItems。图形的内容通过RadioItems功能相应地更新为update_graph选择的选项。到目前为止,一切都很好。当我缩放/平移图表然后更改RadioItems选项时,会出现此问题:该图表已正确更新,但丢失了我之前所做的缩放/平移。如果显示/隐藏两条迹线之一,然后更改RadioItems选项,则会出现相同的问题:图形已更新,但迹线的可视化选项已重置。此行为归因于我拥有的代码:当用户更改RadioItems选项时,称为重置图形的函数update_graph,因此诸如平移/缩放或隐藏/显示跟踪选项之类的属性会丢失。我想freeze这些可视化选项,以便在用户平移/缩放然后更改RadioItems选项时,图形会正确更新,但可以保持用户之前进行的平移/缩放。跟踪隐藏/显示选项相同。我认为有一种方法可以在一个变量中保存当前显示的区域,并在另一个变量中保存轨迹的可见性,并在update_graph中调用此变量,但是我不知道如何保存和调用这些属性。

版本信息:

Python                  3.7.0
dash                    1.12.0
dash-core-components    1.10.0
dash-html-components    1.0.3
dash-renderer           1.4.1
plotly                  4.8.1
python zoom visualization plotly-dash pan
1个回答
0
投票
我相信您正在寻找的是uirevision属性(有关详细信息,请参见the documentation)。要保持缩放,平移等,请在更新图形时将此属性设置为恒定值,即

fig['layout']['uirevision'] = 'some-constant'

每当您需要重置视图时,将其值更改为其他值。    
© www.soinside.com 2019 - 2024. All rights reserved.