Home > Software engineering >  how to merge two graphs into one?
how to merge two graphs into one?

Time:07-03

i have two plotly graphs and idk how to combine them on one chart

import plotly.graph_objects as go
import pandas as pd
from datetime import datetime


df1 = pd.read_csv("CPB1.csv")
fig1 = go.Figure(data=[go.Candlestick(x=df['date'],
                open=df['open'],
                high=df['high'],
                low=df['low'],
                close=df['close'])])
fig1.update_layout(xaxis_rangeslider_visible=False)
fig.show()

df2 = pd.read_csv("CPB1.csv")
fig2 = go.Figure(data=[go.Candlestick(x=df['date'],
                open=df['open'],
                high=df['high'],
                low=df['low'],
                close=df['close'])])
fig2.update_layout(xaxis_rangeslider_visible=False)
fig2.show()

I believe that when deriving, you just need to add them up, but I don’t know how to do it

CodePudding user response:

I understand that you want to draw two candlestick graphs on one graph, so I took two stock prices and graphed them. The technique is to create a graph object and simply add a candlestick for each.

import yfinance as yf
import plotly.graph_objects as go
import numpy as np

df1 = yf.download("AAPL", start="2021-01-01", end="2021-03-01")
df2 = yf.download("MSFT", start="2021-01-01", end="2021-03-01")
df1.reset_index(inplace=True)
df2.reset_index(inplace=True)
df1.columns =['date', 'open', 'high', 'low', 'close', 'adj close', 'volume']
df2.columns =['date', 'open', 'high', 'low', 'close', 'adj close', 'volume']
fig = go.Figure()

fig.add_trace(go.Candlestick(x=df1['date'],
                             open=df1['open'],
                             high=df1['high'],
                             low=df1['low'],
                             close=df1['close'],
                             name='AAPL'))

fig.add_trace(go.Candlestick(x=df2['date'],
                             open=df2['open'],
                             high=df2['high'],
                             low=df2['low'],
                             close=df2['close'],
                             increasing_line_color='blue',
                             name='GOOG'))

fig.update_layout(
    height=500,
    xaxis_rangeslider_visible=False
                 )
fig.show()

enter image description here

  • Related