0

我正在尝试在 Dash 框架内创建一个交互式图表。我是这种设置的新手,因此我从简单的开始,通过重新创建入门指南中的“更多关于可视化”散点图,并稍微添加了一个低回归。期望的结果是样本图与添加的拟合回归保持一致。我的代码是:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import statsmodels.api as sm

app = dash.Dash()

df = pd.read_csv(
    'https://gist.githubusercontent.com/chriddyp/' +
    '5d1ea79569ed194d432e56108a04d188/raw/' +
    'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+
    'gdp-life-exp-2007.csv')

performance_line = pd.DataFrame(sm.nonparametric.lowess(df['life expectancy'], df['gdp per capita'], frac=0.75))

app.layout = html.Div([
    dcc.Graph(
        id='life-exp-vs-gdp',
        figure={
            'data': [
                go.Scatter(
                        x = performance_line[0],
                        y = performance_line[1],
                        mode = 'lines',
                        line = dict(
                            width=0.5
                                ), 
                        name = 'Fit'
                        ),
                go.Scatter(
                    x=df[df['continent'] == i]['gdp per capita'],
                    y=df[df['continent'] == i]['life expectancy'],
                    text=df[df['continent'] == i]['country'],
                    mode='markers',
                    opacity=0.7,
                    marker={
                        'size': 15,
                        'line': {'width': 0.5, 'color': 'white'}
                    },
                    name=i
                ) for i in df.continent.unique()
            ],
            'layout': go.Layout(
                xaxis={'type': 'log', 'title': 'GDP Per Capita'},
                yaxis={'title': 'Life Expectancy'},
                margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
                legend={'x': 0, 'y': 1},
                hovermode='closest'
            )
        }
    )
])

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

由于散点图后的 for 循环,此代码将不起作用。我尝试将它包含在 () 和 [] 中,但是 JSON 子例程无法处理生成器,并且 [] 停止了中断,但实际上并没有绘制散点图。我怎样才能通过额外的低回归得到这个图?

4

1 回答 1

0

在我看来,这像是一个语法问题,列表推导具有以下格式(请原谅简单性):

[(something with i) for i in (iterable)]

而你正在尝试的看起来像

[(unrelated item), (something with i) for i in (iterable)]

以下轻微修改应该可以工作:

[(unrelated item)]+[(something with i) for i in (iterable)]

所以最终的代码将是这样的。

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import statsmodels.api as sm

app = dash.Dash()

df = pd.read_csv(
    'https://gist.githubusercontent.com/chriddyp/' +
    '5d1ea79569ed194d432e56108a04d188/raw/' +
    'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+
    'gdp-life-exp-2007.csv')

performance_line = pd.DataFrame(sm.nonparametric.lowess(df['life expectancy'], df['gdp per capita'], frac=0.75))

app.layout = html.Div([
    dcc.Graph(
        id='life-exp-vs-gdp',
        figure={
            'data': [
                go.Scatter(
                    x = performance_line[0],
                    y = performance_line[1],
                    mode = 'lines',
                    line = dict(
                        width=0.5
                            ), 
                    name = 'Fit'
                )
            ]+[
                go.Scatter(
                    x=df[df['continent'] == i]['gdp per capita'],
                    y=df[df['continent'] == i]['life expectancy'],
                    text=df[df['continent'] == i]['country'],
                    mode='markers',
                    opacity=0.7,
                    marker={
                        'size': 15,
                        'line': {'width': 0.5, 'color': 'white'}
                    },
                    name=i
                ) for i in df.continent.unique()
            ],
            'layout': go.Layout(
                xaxis={'type': 'log', 'title': 'GDP Per Capita'},
                yaxis={'title': 'Life Expectancy'},
                margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
                legend={'x': 0, 'y': 1},
                hovermode='closest'
            )
        }
    )
])

if __name__ == '__main__':
    app.run_server()
于 2018-03-17T21:30:47.650 回答