PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] how to merge two graphs into one?

 August 29, 2022     csv, plotly, python     No comments   

Issue

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


Solution

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



Answered By - r-beginners
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing