【问题标题】:How to serve an animated plotly-express figure in dash?如何在破折号中提供动画情节表达图?
【发布时间】:2019-07-25 19:03:49
【问题描述】:

我尝试在 dash 中显示 plotly-exress 主页中的这个示例。

import plotly.express as px
gapminder = px.data.gapminder()
px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

像这样:

import plotly_express as px
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output

gapminder = px.data.gapminder()

dimensions = []

app = dash.Dash(
    __name__, external_stylesheets=["https://codepen.io/chriddyp/pen/bWLwgP.css"]
)

app.layout = html.Div(
    [
        html.H1("Gapminder data"),
        dcc.Graph(id="graph", style={"width": "75%", "display": "inline-block"}),
    ]
)

@app.callback(Output("graph", "figure"), [])
def make_figure():
    return px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
                      size="pop", color="continent", hover_name="country",
                      log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])


app.run_server(debug=True)

不过,只出现了一个空的身影。

【问题讨论】:

    标签: plotly plotly-dash plotly-express


    【解决方案1】:

    编辑:您需要最新版本的 Dash 和 Dash 核心组件来渲染动画帧。

    在这种情况下,这是因为您的回调永远不会触发,因为第二个参数是一个空列表(即它不依赖于任何输入)。

    如果将图形内联到布局中,它将呈现:

    import plotly_express as px
    import dash
    import dash_html_components as html
    import dash_core_components as dcc
    from dash.dependencies import Input, Output
    
    gapminder = px.data.gapminder()
    
    dimensions = []
    
    app = dash.Dash(
        __name__, external_stylesheets=["https://codepen.io/chriddyp/pen/bWLwgP.css"]
    )
    
    app.layout = html.Div(
        [
            html.H1("Gapminder data"),
            dcc.Graph(id="graph", style={"width": "75%", "display": "inline-block"},
            figure=px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
                          size="pop", color="continent", hover_name="country",
                          log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])),
        ]
    )
    
    
    app.run_server(debug=True)
    

    或者你可以连接你的回调来依赖一个确实改变的输入,它会触发并填充图形:)

    【讨论】:

    • 您使用的是最新版本的dash,即1.0.x,以及对应版本的dash_core_components吗?这些一起为我工作。几周前,我向 dcc.Graph 添加了框架支持。
    • 不,我正在使用“0.39.0”,这似乎是 conda 频道上可用的最新版本... 为什么我必须指定 dash=1 才能获得最新版本?而是安装了这个过时的版本...
    • 我不知道为什么 conda 会这样做,抱歉 :) 可能是因为它是从 0.x 到 1.x 的主要版本更改?
    • 如果您不想一直使用 1.0,我检查并在 0.48 版中将框架支持添加到 dash-core-components:github.com/plotly/dash-core-components/blob/master/CHANGELOG.md
    • 太棒了!如果对您有帮助,请随时接受答案:)
    猜你喜欢
    • 2021-02-20
    • 2022-01-23
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2021-12-20
    • 2021-04-07
    相关资源
    最近更新 更多