图未保存在函数中。我该如何解决?

时间:2019-02-01 13:09:42

标签: python python-2.7 plotly plotly-dash

我有一个函数,使用matplotlib绘图,并使用tls.matplotlib_to_pyplot将其转换为绘图。最后,我将其命名为plotly_fig。现在,我正在尝试将滑杆添加到破折号的webapp中。但是,当我编译它时,出现一条错误消息,指出未定义plotly_fig。 以下是一些示例代码,可重新创建错误。

import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
from numpy.linalg import matrix_power
import matplotlib.pyplot as plt
import plotly.tools as tls
from mpl_toolkits.mplot3d import Axes3D
from dash.dependencies import Input, Output
app = dash.Dash()
#begin with the knobs
app.layout = html.Div([
   dcc.Graph(
       id = 'graph',
       figure = plotly_fig),

   html.Label('Config L'), ## this is the knob for the length
   dcc.Slider(
       id = 'l',
       min = 5,
       max = 10,
       marks = {i: 'Label ={}'.format(i) if i == 1 else str(i) for i in range(5,10)},
       value = L,
   ),

   html.Label('Config n'), ##knob for the n-gon
   dcc.Slider(
       id = 'n',
       min = 0,
       max = 10,
       marks = {i: 'Label ={}'.format(i) if i == 1 else str(i) for i in range(1,10)},
       value = n,
   ),

   html.Label('Config N'),  ##knob for the number of n-gons outside initial
   dcc.Slider(
       id = 'N',
       min = 0,
       max = 10,
       marks = {i: 'Label ={}'.format(i) if i == 1 else str(i) for i in range(1,10)},
       value = N,
   ),

   html.Label('Config r'),  ##knob for r only works on integers for now
   dcc.Slider(
       id = 'r',
       min = 0,
       max = 2,
       marks = {i: 'Label ={}'.format(i) if i == 1 else str(i) for i in    range(1,2)},
       value = r,
   ),

   html.Label('Config d'), ##knoc for the depth of the dip
   dcc.Slider(
       id = 'd',
       min = 0,
       max = 2,
       marks = {i: 'Label ={}'.format(i) if i == 1 else str(i) for i in range(1,2)},
       value = d,
)
],
style = {'columnCount': 1})
@app.callback(
   dash.dependencies.Output('graph', 'figure'),
   [dash.dependencies.Input('l', 'value'),
   dash.dependencies.Input('n', 'value'),
   dash.dependencies.Input('N', 'value'),
   dash.dependencies.Input('r', 'value'),
   dash.dependencies.Input('d', 'value')])

def output(L,n,N,r,d):
    x = np.linspace(np.pi, L*n*N*r*d*np.pi, 1000)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(x, sinx)
    plotly_fig = tls.mpl_to_plotly(mpl_fig)
    return{plotly_fig}
if __name__=='__main__':
    app.run_server(debug = True)

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

问题在于,当您尝试在图形的plotly_fig字段中使用变量figure时,甚至没有声明变量figure。它只是在回调中本地声明的。

不需要显式设置Graph的#your code here app = dash.Dash() #begin with the knobs app.layout = html.Div([ dcc.Graph( id = 'graph'), # ==> here you can remove the figure as it will be automatically set during the callback. #your code here ], style = {'columnCount': 1}) @app.callback( dash.dependencies.Output('graph', 'figure'), #here figure represents the field [dash.dependencies.Input('l', 'value'), dash.dependencies.Input('n', 'value'), dash.dependencies.Input('N', 'value'), dash.dependencies.Input('r', 'value'), dash.dependencies.Input('d', 'value')]) def output(L,n,N,r,d): #your code here plotly_fig = tls.mpl_to_plotly(mpl_fig) return{plotly_fig} if __name__=='__main__': app.run_server(debug = True) 属性,它将在回调期间自动映射,因此您可以直接这样做,

value

在上述示例中,Slider的figure属性是应用程序的输入,而应用程序的输出则是Graph的output属性。每当Slider的值更改时,Dash就会使用新的输入值调用回调函数plotly_fig。该函数使用此新值过滤数据框,构造一个图形对象,并将其返回给Dash应用程序。

同时,如果您希望在调用回调函数之前就设置默认值,可以将#your code here plotly_fig = None # declare the default figure here app = dash.Dash() #begin with the knobs app.layout = html.Div([ dcc.Graph( id = 'graph', figure = plotly_fig), #your code here ], style = {'columnCount': 1}) @app.callback( dash.dependencies.Output('graph', 'figure'), [dash.dependencies.Input('l', 'value'), dash.dependencies.Input('n', 'value'), dash.dependencies.Input('N', 'value'), dash.dependencies.Input('r', 'value'), dash.dependencies.Input('d', 'value')]) def output(L,n,N,r,d): #your code here plotly_fig = tls.mpl_to_plotly(mpl_fig) return{plotly_fig} if __name__=='__main__': app.run_server(debug = True) 声明为这样的全局变量,

space-separated csv

有关更多信息,请参见带有类似示例的官方文档页面,

https://dash.plot.ly/getting-started-part-2