【问题标题】:Python - Lowess regression integration for Plotly DashPython - Plotly Dash 的 Lowess 回归集成
【发布时间】:2018-07-06 00:49:07
【问题描述】:

我正在尝试在 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 子例程无法处理生成器,并且 [] 停止了中断但实际上并未绘制散点图。我怎样才能通过额外的低回归得到这个图表?

【问题讨论】:

  • 您是否尝试过仅先使用 plotly 绘制它?而且我想问题出在go.Scatter 尝试使用字典和'type':'scatter'

标签: python data-visualization plotly plotly-dash


【解决方案1】:

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

[(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()

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2020-03-07
    • 2019-03-12
    • 2020-03-17
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多