Search code examples
pythonplotlystock

Pyplot - Plotting multiple distribution from a dataframe


I am analysing a stock portfolio. I downloaded data from yahoo finance and created a data frame. What I want to do now is analyse and plot simple returns and log returns distributions and i want to be able to do it for one stock, but also (and here is my question) to plot all the stocks' distribution in the same graph so to spot their different behaviours. I can plot the single stock return distribution but not the multiple graphs one.

#Import libraries
import numpy as np 
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
from pandas_datareader import data as pdr

#Set start/end time and get stocks data from Yahoo Finance
end = dt.datetime.now()
start = end - dt.timedelta(weeks=104)
stocks_list = ['COST', 'NIO', 'AMD']
df = pdr.get_data_yahoo(stocks_list, start, end)

#Rename 'Adj Close' to be able to create a more accessible variable
df.rename(columns = {"Adj Close" : "Adj_Close"}, inplace = True)
AClose = df.Adj_Close

#Import plotly 
import plotly.offline as pyo
pyo.init_notebook_mode(connected=True)
pd.options.plotting.backend = 'plotly'

#Plot the Adjusted Close price with plotly
AClose.plot()

#Plot the SIMPLE return distrib. chart of the Adj_Close price of 'COST' with plotly
AClose['COST'].pct_change().plot(kind='hist')

#Plot the SIMPLE return distrib. chart of the Adj_Close price of ALL the stocks
**QUESTION 1**

#Compute log returns
log_returns = np.log(df.Adj_Close/df.Adj_Close.shift(1)).dropna()

#Plot the LOG returns distrib. chart of the Adj_Close price of 'COST'
log_returns['COST'].plot(kind = 'hist')

#Plot the LOG return distrib. chart of the Adj_Close price of ALL the stocks
**QUESTION 2**

What I am trying to achieve is something like this, but I want to plot all the stocks and not just one and I want to do it with plotly so i can toggle the stocks data in & out from the graph's legend. Normal Vs Stock return


Solution

  • I have created the code with the understanding that you want to create a histogram with all returns in different colors. I think all I need to do is add a normal distribution graph in line mode for scatter plots. Simple returns can also be graphed using the same technique. I think that each comparison should consist of subplots.

    log_max = log_returns.max().max()
    log_min = log_returns.min().min()
    
    import plotly.graph_objects as go
    
    fig = go.Figure()
    fig.add_trace(go.Histogram(x=log_returns.COST, name='COST'))
    fig.add_trace(go.Histogram(x=log_returns.NIO, name='NIO'))
    fig.add_trace(go.Histogram(x=log_returns.AMD, name='AMD'))
    
    fig.update_xaxes(range=[log_min, log_max])
    fig.update_layout(autosize=True, height=450, barmode='overlay', title='Log returns: COST,NIO,AMD')
    fig.update_traces(opacity=0.5)
    fig.show()
    

    enter image description here