Python - Plotly Dash的Lowess回归集成

时间:2018-01-26 16:25:12

标签: python data-visualization plotly plotly-dash

我正在尝试在Dash框架内创建交互式图形。我是这种类型的设置的新手,因此我开始简单地通过重新创建"更多关于可视化"在getting started guide中找到的散点图,稍微添加了一个低值回归。期望的结果是样本图保持与添加的拟合回归相同。我的代码是:

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子程序不能处理生成器而[]停止中断,但实际上并没有绘制散点图。如何使用额外的lowess回归得到此图?

1 个答案:

答案 0 :(得分: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()