添加多项式趋势线 [PYTHON]

Add Polynomial Trend Line Plotly [PYTHON]

我正在尝试将多项式趋势线添加到具有 2 个 y 轴的 plotly go 图表中,趋势线应遵循绘制每周柱相对于时间的轨迹。

该图表的目的是查看随时间变化的结果,并确定引入新变量时数据集中的变化。

数据集:

date  weekly partially fully
3/28/20     2   0   0
4/4/20      5   0   0
4/11/20     3   0   0
4/18/20     6   0   0
4/25/20     2   0   0
5/2/20      3   0   0
5/9/20      5   0   0
5/16/20     1   0   0
5/23/20     0   0   0
5/30/20     2   0   0
6/6/20      2   0   0
6/13/20     0   0   0
6/20/20     0   0   0
6/27/20     0   0   0
7/4/20      0   0   0
7/11/20     0   0   0
7/18/20     0   0   0
7/25/20     1   0   0
8/1/20      0   0   0
8/8/20      0   0   0
8/15/20     0   0   0
8/22/20     1   0   0
8/29/20     1   0   0
9/5/20      1   0   0
9/12/20     0   0   0
9/19/20     0   0   0
9/26/20     0   0   0
10/3/20     0   0   0
10/10/20    0   0   0
10/17/20    0   0   0
10/24/20    4   0   0
10/31/20    4   0   0
11/7/20     15  0   0
11/14/20    6   0   0
11/21/20    2   0   0
11/28/20    1   0   0
12/5/20     4   0   0
12/12/20    4   0   0
12/19/20    7   0   0
12/26/20    0   0   0
1/2/21      7   406 0
1/9/21      5   406 0
1/16/21     11  406 0
1/23/21     8   242 395
1/30/21     2   241 396
2/6/21      5   241 396
2/13/21     4   22  615
2/20/21     6   81  625
2/27/21     2   98  625
3/6/21      3   98  625
3/13/21     1   98  625
3/20/21     2   85  693
3/27/21     5   84  709
4/3/21      5   84  710
4/10/21     2   84  760
4/17/21     3   31  849

我知道这可以用 plotly express 来完成,但它不支持我能找到的双轴,由于值的数量级差异,我需要双轴。

到目前为止,我在无聊的 google 搜索中遇到的所有支持 material 都非常复杂,因为我知道在电子表格包中非常简单,例如 excel.

到目前为止,这是我的代码:

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces

fig.add_trace(go.Scatter(x=df['date'], y=df['full'], name="Indicator 2",line=dict(color="Green"),opacity=0.01,fill='tozeroy',), secondary_y=True,)

fig.add_trace(go.Scatter(x=df['date'], y=df['weekly'], name="Weekly",line=dict(color="Red")),secondary_y=False,)

# Add figure title
fig.update_layout(
    title_text="Change over Time",width = 1200, height = 600)

# Set x-axis title
fig.update_xaxes(title_text="xaxis title")

# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis1", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis2", secondary_y=True)

fig.show()

这是当前图形输出:

非常感谢。

  • 合成数据类似于没有OCR就不能使用的数据
  • 您可以使用 numpy 计算多项式,然后绘制另一条线
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np

d = pd.date_range("28-mar-2020", freq="W", periods=60)
df = pd.DataFrame({"date":d, "weekly":np.random.randint(0,15,len(d)), "partial":np.random.randint(31,406,len(d)),
              "full":np.random.randint(395,850,len(d))
             }).assign(partial=lambda x: np.where(x.date.dt.year<2021,0, x.partial),
                      full=lambda x: np.where(x.date.lt("1-Feb-2021"),0, x.full))


df["smooth"] = np.polyval(np.polyfit(df["date"].astype(int),df["weekly"], 5), df["date"].astype(int))

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces

fig.add_trace(go.Scatter(x=df['date'], y=df['full'], name="Indicator 2",line=dict(color="Green"),opacity=0.01,fill='tozeroy',), secondary_y=True,)

fig.add_trace(go.Scatter(x=df['date'], y=df['weekly'], name="Weekly",line=dict(color="Red")),secondary_y=False,)
fig.add_trace(go.Scatter(x=df['date'], y=df['smooth'], name="Smooth",line=dict(color="Blue")),secondary_y=False,)


# Add figure title
fig.update_layout(
    title_text="Change over Time",width = 1200, height = 600)

# Set x-axis title
fig.update_xaxes(title_text="xaxis title")

# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis1", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis2", secondary_y=True)