plotly - 如何在 dash 中提供动画 plotly 表达人物?

标签 plotly plotly-dash plotly-express

我尝试在 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)

但是,只出现了一个空的数字。

最佳答案

编辑:您需要最新版本的 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)

或者您可以连接回调以依赖于确实发生变化的输入,它将触发并填充图形:)

关于plotly - 如何在 dash 中提供动画 plotly 表达人物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208663/

相关文章:

javascript - 单击 div 中除超链接之外的所有元素时如何切换 div 的显示?

Python Dash 绘图 : Show default closest data on hover or compare data on hover in graph

Python Plotly Express : How to conditionally fill an area plot?

python - 如何使用 Python 在 map 上绘制可视化线串?

python - 悬停时仅显示单行,隐藏所有其他行

python - 访问笔记本中的绘图图形重新布局数据

python - Jupyter Lab交互式绘图: unsupported operand type(s) for *: 'FloatSlider' and 'float'

javascript - Plotly-Dash:如何确定客户端回调中的触发器输入

python - Plotly 中的 Seaborn HUE

python - 使用显示的 Plotly Express 小部件保存 Jupyter Notebook