Pandas Plotly -- 二级图需要是 RSI

Pandas Plotly -- Secondary Graph needs to be to RSI

在plotly中,我可以用下面的代码制作这个图(最后附上图片)。

(数据为1年的股市数据,csv格式。请使用任何OHLC数据,大约有200到300行)

import pandas as pd
import ta
import plotly.graph_objects as go

df = pd.read_csv("Trial.csv")
df["rsi"] = ta.momentum.rsi(df["Close"], window=14, fillna=False)
dfff = df.tail(180)

layoutt = go.Layout(autosize=False, width=4181, height=1597)

fig_001 = go.Figure(data=[go.Candlestick(x=dfff['Date'], open=dfff['Open'], high=dfff['High'], low=dfff['Low'], close=dfff['Close'])], layout=layoutt)
fig_001.write_image("fig_001.jpeg")

如下图所示,Plotly 默认生成 2 个图表(下面有一个较小的重复图表)...

关于包含在 'Green' 中的辅助图表,我如何将其更改为 RSI 图表((目前与红色的烛台数据相同))?

  • plotly 没有生成两个图表。它是一个带有范围滑块的(交互时可以用来放大 xaxis)
  • 有隐藏范围滑块
  • 已创建额外的跟踪并将其设置为使用第二个 yaxis
  • 已将 yaxes 配置为使用域,因此它具有相同的视觉效果
import pandas as pd
import ta
import plotly.graph_objects as go

# df = pd.read_csv("Trial.csv")
# use plotly OHLC sample data
df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv"
)
df = df.rename(columns={c: c.split(".")[1] for c in df.columns if "." in c})
df["rsi"] = ta.momentum.rsi(df["Close"], window=14, fillna=False)
dfff = df.tail(180)

layoutt = go.Layout(autosize=False, width=4181, height=1597)
# make it fit on my screen!!!
layoutt = go.Layout(autosize=True)
layoutt2 = go.Layout(autosize=False, width=6731, height=2571)
fig_001 = go.Figure(
    data=[
        go.Candlestick(
            x=dfff["Date"],
            open=dfff["Open"],
            high=dfff["High"],
            low=dfff["Low"],
            close=dfff["Close"],
            name="OHLC",
        ),
        go.Scatter(
            x=dfff["Date"], y=dfff["rsi"], mode="markers+lines", name="RSI", yaxis="y2"
        ),
    ],
    layout=layoutt,
).update_layout(
    yaxis_domain=[0.3, 1],
    yaxis2={"domain": [0, 0.20]},
    xaxis_rangeslider_visible=False,
    showlegend=False,
)
fig_001