使用Plotly在Python中创建的3D动画线图。

huangapple go评论78阅读模式
英文:

3d animated line plot with plotly in python

问题

我看到了这个3D图。它是动画的,每天都会添加一个新的值。我没有找到使用Python中的Plotly重新创建它的示例。

使用Plotly在Python中创建的3D动画线图。

图应该从第一行的值(100)开始。起始值应该保持不变(不要滚动值)。图应以这样的方式进行动画,即每一行的值都会一个接一个地添加,x轴会扩展。以下数据框包含要绘制的值(df_stocks)和日期。分配颜色将是一个很好的补充。正值越多,绿色越深,负值越多,红色越深。

import yfinance as yf
import pandas as pd

stocks = ["AAPL", "MSFT"]

df_stocks = pd.DataFrame()
for stock in stocks:
    df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
    df['perct'] = df['Close'].pct_change()
    df_stocks[stock] = df['perct']

df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100 
英文:

I saw this 3d plot. it was animated and added a new value every day. i have not found an example to recreate it with plotly in python.

使用Plotly在Python中创建的3D动画线图。

the plot should start with the value from the first row (100). The start value should remain (no rolling values). The plot should be animated in such a way that each row value is added one after the other and the x-axis expands. the following data frame contains the values (df_stocks) and Dates to plot. assigning the colors would be a great addition. the more positive the deeper the green, the more negative the darker red.

import yfinance as yf
import pandas as pd

stocks = ["AAPL", "MSFT"]

df_stocks = pd.DataFrame()
for stock in stocks:
    df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
    df['perct'] = df['Close'].pct_change()
    df_stocks[stock] = df['perct']

df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100 

答案1

得分: 1

以下是您要翻译的内容:

You can use a list of go.Frame objects as shown in this example. Since you want the line plot to continually extend outward, each frame needs to include data that's one row longer than the previous frame, so we can use a list comprehension like:

frames = [go.Frame(data=

    ## ...extract info from df_stocks.iloc[:i]

for i in range(len(df_stocks))]

To add colors to your lines depending on their value, you can use binning and labels (as in this answer) to create new columns called AAPL_color and MSFT_color that contain the string of the css color (like 'darkorange' or 'green'). Then you can pass the information from these columns using the argument line=dict(color=...) in each go.Scatter3d object.

import yfinance as yf
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

stocks = ["AAPL", "MSFT"]

df_stocks = pd.DataFrame()
for stock in stocks:
    df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
    df['perct'] = df['Close'].pct_change()
    df_stocks[stock] = df['perct']

df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100 

df_min = df_stocks[['AAPL','MSFT']].min().min() - 1
df_max = df_stocks[['AAPL','MSFT']].max().max() + 1

labels = ['firebrick','darkorange','peachpuff','palegoldenrod','palegreen','green']
bins = np.linspace(df_min,df_max,len(labels)+1)
df_stocks['AAPL_color'] = pd.cut(df_stocks['AAPL'], bins=bins, labels=labels).astype(str)
df_stocks['MSFT_color'] = pd.cut(df_stocks['MSFT'], bins=bins, labels=labels).astype(str)

frames = [go.Frame(
    data=[
        go.Scatter3d(
            y=df_stocks.iloc[:i].index, 
            z=df_stocks.iloc[:i].AAPL.values,
            x=['AAPL']*i,
            name='AAPL',
            mode='lines',
            line=dict(
                color=df_stocks.iloc[:i].AAPL_color.values, width=3,
            )
        ), 
        go.Scatter3d(
            y=df_stocks.iloc[:i].index, 
            z=df_stocks.iloc[:i].MSFT.values,
            x=['MSFT']*i,
            name='MSFT',
            mode='lines',
            line=dict(
                color=df_stocks.iloc[:i].MSFT_color.values, width=3,
            )
        )]
    ) 
for i in range(len(df_stocks))]

fig = go.Figure(
    data=list(frames[1]['data']),
    frames=frames,
    layout=go.Layout(
        # xaxis=dict(range=[0, 5], autorange=False),
        # yaxis=dict(range=[0, 5], autorange=False),
        # zaxis=dict(range=[0, 5], autorange=False),
        template='plotly_dark',
        legend = dict(bgcolor = 'grey'),
        updatemenus=[dict(
            type="buttons",
            font=dict(color='black'),
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
    ),
)

fig.show()

使用Plotly在Python中创建的3D动画线图。

英文:

You can use a list of go.Frame objects as shown in this example. Since you want the line plot to continually extend outward, each frame needs to include data that's one row longer than the previous frame, so we can use a list comprehension like:

frames = [go.Frame(data=

    ## ...extract info from df_stocks.iloc[:i]

for i in range(len(df_stocks))]

To add colors to your lines depending on their value, you can use binning and labels (as in this answer) to create new columns called AAPL_color and MSFT_color that contain the string of the css color (like 'darkorange' or 'green'). Then you can pass the information from these columns using the argument line=dict(color=...) in each go.Scatter3d object.

import yfinance as yf
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

stocks = ["AAPL", "MSFT"]

df_stocks = pd.DataFrame()
for stock in stocks:
    df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
    df['perct'] = df['Close'].pct_change()
    df_stocks[stock] = df['perct']

df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100 

df_min = df_stocks[['AAPL','MSFT']].min().min() - 1
df_max = df_stocks[['AAPL','MSFT']].max().max() + 1

labels = ['firebrick','darkorange','peachpuff','palegoldenrod','palegreen','green']
bins = np.linspace(df_min,df_max,len(labels)+1)
df_stocks['AAPL_color'] = pd.cut(df_stocks['AAPL'], bins=bins, labels=labels).astype(str)
df_stocks['MSFT_color'] = pd.cut(df_stocks['MSFT'], bins=bins, labels=labels).astype(str)

frames = [go.Frame(
    data=[
        go.Scatter3d(
            y=df_stocks.iloc[:i].index, 
            z=df_stocks.iloc[:i].AAPL.values,
            x=['AAPL']*i,
            name='AAPL',
            mode='lines',
            line=dict(
                color=df_stocks.iloc[:i].AAPL_color.values, width=3,
            )
        ), 
        go.Scatter3d(
            y=df_stocks.iloc[:i].index, 
            z=df_stocks.iloc[:i].MSFT.values,
            x=['MSFT']*i,
            name='MSFT',
            mode='lines',
            line=dict(
                color=df_stocks.iloc[:i].MSFT_color.values, width=3,
            )
        )]
    ) 
for i in range(len(df_stocks))]

fig = go.Figure(
    data=list(frames[1]['data']),
    frames=frames,
    layout=go.Layout(
        # xaxis=dict(range=[0, 5], autorange=False),
        # yaxis=dict(range=[0, 5], autorange=False),
        # zaxis=dict(range=[0, 5], autorange=False),
        template='plotly_dark',
        legend = dict(bgcolor = 'grey'),
        updatemenus=[dict(
            type="buttons",
            font=dict(color='black'),
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
    ),
)

fig.show()

使用Plotly在Python中创建的3D动画线图。

huangapple
  • 本文由 发表于 2023年2月16日 03:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定