Home > front end >  Pandas Plotly -- Secondary Graph needs to be to RSI
Pandas Plotly -- Secondary Graph needs to be to RSI

Time:03-10

In plotly, I am able to make this graph(Attached picture at the end), with the code below.

(Data is stock market data for 1year, which is in csv format. Please use any OHLC data which has about 200 to 300 rows)

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

As you see in the attached picture below, Plotly is generating 2 charts by default (with a smaller-duplicated chart below)...

About the secondary graph which is enclosed in 'Green', how I change that to a RSI graph((Which is currently the same candlestick data as in red))?

Output Graph

CodePudding user response:

  • plotly is not generating two charts. It is one with a range slider (when interactive can use to zoom into xaxis)
  • have hidden range slider
  • have created an additional trace and set it to use a second yaxis
  • have configure yaxes to use domains so it has same visual effect
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

enter image description here

  • Related