Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在plotly dash上创建条形图时遇到的问题_Python_Anaconda_Data Science_Plotly Dash - Fatal编程技术网

Python 在plotly dash上创建条形图时遇到的问题

Python 在plotly dash上创建条形图时遇到的问题,python,anaconda,data-science,plotly-dash,Python,Anaconda,Data Science,Plotly Dash,代码: 我必须用plotly-dash做一个条形图。但我没有得到所需的产出,我无法理解为什么会出现这样的结果。 我得到的输出是: 有人能告诉我代码有什么问题吗?我想你没有得到图表的原因是你缺少了go.Figure()。由于我没有您正在使用的相同数据,我使用了官方网站上使用的数据,并修改了下拉列表值以匹配数据。我验证的环境是JupyterLab,因此有一些代码,但请忽略它 app.layout=html.Div([ html.Div('WORLD HAPPINESS REPORT

代码:

我必须用plotly-dash做一个条形图。但我没有得到所需的产出,我无法理解为什么会出现这样的结果。 我得到的输出是:


有人能告诉我代码有什么问题吗?

我想你没有得到图表的原因是你缺少了
go.Figure()
。由于我没有您正在使用的相同数据,我使用了官方网站上使用的数据,并修改了下拉列表值以匹配数据。我验证的环境是JupyterLab,因此有一些代码,但请忽略它

    app.layout=html.Div([
    html.Div('WORLD HAPPINESS REPORT',
            style={
                'textAlign':'center',
                'color':'#F197C0',
                'fontWeight':'bold',
                'fontSize':'50px',
                'fontFamily':'Sans-serif',
                'border':({"width":"2px", "color":"black"})
            }),
    html.P('''The World Happiness Report is an annual report on the state of global happiness
and is currently participated by 155 countries. ''',
           style={
                'textAlign':'justify-text',
                'color':'#B49FDC',
                'fontWeight':'bold',
                'fontSize':'15px',
                'fontFamily':'Sans-serif'
            }),
    #for dropdown
    html.Br(),
    dcc.Dropdown(id='select_year',
                options=[
                    {'label':'2015','value':2015},
                    {'label':'2016','value':2016},
                    {'label':'2017','value':2017},
                    {'label':'2018','value':2018},
                    {'label':'2019','value':2019},
                    {'label':'2020','value':2020},
                ],
                 multi=False,
                 value='2015',
                 style={'width': "40%"}
                 
                ), 
    
    dcc.Graph(id='the_graph',figure={})

])

@app.callback(
     Output(component_id='the_graph', component_property='figure'),
    [Input(component_id='select_year', component_property='value')] #this is for the argument of the func.written below.
)
def update_graph(option_selected):
    #print(option_slctd)
    #print(type(option_slctd))

    #container = "The year chosen by user was: {}".format(option_slctd)
    #if option_selected==2015:
        
    d1=data[data['Year']==option_selected]
    fig=go.Bar(
        x=df1['Country'],
        y=df1['Happiness Score']

        )
        
    return fig

仍然没有生成图形。我甚至复制了spider上的代码并在那里运行,但仍然没有生成图形。我不使用spider,所以我不知道原因。您能在另一个开发环境中检查它吗?例如,VSCode?我也在anaconda上做过,但仍然不起作用。只是为了确定,你已经复制了我所有的代码并检查过了,对吗?官方参考中的示例代码在Anaconda环境中是否显示良好?如果没有,请尝试。一切都很好。但仍然不起作用
import dash
import dash_core_components as dcc
import dash_html_components as html
#from jupyter_dash import JupyterDash
from dash.dependencies import Input, Output, State
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

data = px.data.gapminder()

#app = JupyterDash(__name__)
app = dash.Dash(__name__)

app.layout = html.Div([html.Div('WORLD HAPPINESS REPORT',
                                style={'textAlign':'center',
                                       'color':'#F197C0',
                                       'fontWeight':'bold',
                                       'fontSize':'50px',
                                       'fontFamily':'Sans-serif',
                                       'border':({"width":"2px", "color":"black"})
                                      }),
                       html.P('''The World Happiness Report is an annual report on the state of global happiness 
                       and is currently participated by 155 countries. ''',
                              style={'textAlign':'justify-text',
                                     'color':'#B49FDC','fontWeight':'bold',
                                     'fontSize':'15px',
                                     'fontFamily':'Sans-serif'}),#for dropdown
                       html.Br(),
                       dcc.Dropdown(id='select_year',
                                    options=[
                                        {'label':'1982','value':1982},
                                        {'label':'1987','value':1987},
                                        {'label':'1992','value':1992},
                                        {'label':'1997','value':1997},
                                        {'label':'2002','value':2002},
                                        {'label':'2007','value':2007},],
                                    multi=False,
                                    value='2015',
                                    style={'width': "40%"}),
                       dcc.Graph(id='the_graph',figure={})
])

@app.callback(
     Output(component_id='the_graph', component_property='figure'),
    [Input(component_id='select_year', component_property='value')] #this is for the argument of the func.written below.
)
def update_graph(option_selected):
    df1=data[data['year'] == option_selected]
    fig=go.Figure(data=[go.Bar(x=df1['country'], y=df1['lifeExp'])])
    return fig

if __name__ == "__main__":
    app.run_server()
    #app.run_server(mode='inline')