为了账号安全,请及时绑定邮箱和手机立即绑定

动画线图与 python plotly

动画线图与 python plotly

慕尼黑8549860 2022-12-27 17:10:58
我有黄金价格数据集,其中第一列是 yyyy-mm-dd 格式的日期,第二列是黄金价格。       2019-12-03    1477.60        2019-12-04    1474.45        2019-12-05    1448.40有什么方法可以用 python plotly 制作动画线图,我可以在其中显示黄金价格随日期的变化?
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

是的,我给你举个例子。

效果很花哨,但你并没有得到太多,因为它是一个二维数据,我可以说你确实在无缘无故地延迟数据显示。

通常动画很适合显示 3 个维度,显然使用时间作为额外的维度来执行动画,就像 plotly web 动画文档中的第一个例子:https ://plotly.com/python/animations/

import plotly.graph_objects as go

import pandas as pd


# Maybe you needed to display plot in jupyter notebook

import plotly.offline as pyo

pyo.init_notebook_mode()


# Load exmples data

dates = ["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 plot

fig = 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 traces

init = 1


fig.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")))


# Animation

fig.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 Formatting

fig.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")


# Buttons

fig.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


查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信