猿问

动画线图与 python plotly

我有黄金价格数据集,其中第一列是 yyyy-mm-dd 格式的日期,第二列是黄金价格。

       2019-12-03    1477.60
       2019-12-04    1474.45
       2019-12-05    1448.40

有什么方法可以用 python plotly 制作动画线图,我可以在其中显示黄金价格随日期的变化?


慕尼黑8549860
浏览 107回答 1
1回答

慕运维8079593

是的,我给你举个例子。效果很花哨,但你并没有得到太多,因为它是一个二维数据,我可以说你确实在无缘无故地延迟数据显示。通常动画很适合显示 3 个维度,显然使用时间作为额外的维度来执行动画,就像 plotly web 动画文档中的第一个例子:https ://plotly.com/python/animations/import plotly.graph_objects as goimport pandas as pd# Maybe you needed to display plot in jupyter notebookimport plotly.offline as pyopyo.init_notebook_mode()# Load exmples datadates = ["2019-12-03", "2019-12-04", "2019-12-05", "2019-12-06",         "2019-12-07", "2019-12-08", "2019-12-09"]value_gold = [1477.60, 1474.45, 1448.40, 1447.40, 1444.40, 1449.40, 1441.40]value_bitcoin = [1577.60, 1564.45, 1568.40, 1537.40, 1584.40, 1529.40, 1571.40]df = pd.DataFrame(list(zip(dates, value_gold, value_bitcoin)),                  columns=['date', 'value_gold', 'value_bitcoin'])# Base plotfig = go.Figure(    layout=go.Layout(        updatemenus=[dict(type="buttons", direction="right", x=0.9, y=1.16), ],        xaxis=dict(range=["2019-12-02", "2019-12-10"],                   autorange=False, tickwidth=2,                   title_text="Time"),        yaxis=dict(range=[1400, 1600],                   autorange=False,                   title_text="Price"),        title="Gold - Bitcoin prices evolution",    ))# Add tracesinit = 1fig.add_trace(    go.Scatter(x=df.date[:init],               y=df.value_gold[:init],               name="Gold",               visible=True,               line=dict(color="#33CFA5", dash="dash")))fig.add_trace(    go.Scatter(x=df.date[:init],               y=df.value_bitcoin[:init],               name="Bitcoin",               visible=True,               line=dict(color="#bf00ff", dash="dash")))# Animationfig.update(frames=[    go.Frame(        data=[            go.Scatter(x=df.date[:k], y=df.value_gold[:k]),            go.Scatter(x=df.date[:k], y=df.value_bitcoin[:k])]    )    for k in range(init, len(df)+1)])# Extra Formattingfig.update_xaxes(ticks="outside", tickwidth=2, tickcolor='white', ticklen=10)fig.update_yaxes(ticks="outside", tickwidth=2, tickcolor='white', ticklen=1)fig.update_layout(yaxis_tickformat=',')fig.update_layout(legend=dict(x=0, y=1.1), legend_orientation="h")# Buttonsfig.update_layout(    updatemenus=[        dict(            buttons=list([                dict(label="Play",                        method="animate",                    args=[None, {"frame": {"duration": 1000}}]),                dict(label="Gold",                    method="update",                    args=[{"visible": [False, True]},                          {"showlegend": True}]),                dict(label="Bitcoin",                    method="update",                    args=[{"visible": [True, False]},                          {"showlegend": True}]),                dict(label="All",                    method="update",                    args=[{"visible": [True, True, True]},                          {"showlegend": True}]),            ]))])fig.show()https://i.stack.imgur.com/7fiVq.gif
随时随地看视频慕课网APP

相关分类

Python
我要回答