5.3. plotly#

We’ve now seen the basics of plotting with pandas and matplotlib. We saw seaborn. Let’s try another package, called plotly, that let’s us create interactive graphics. To install the Plotly package, you’ll need to use pip. As before, you can do this inside of a cell in your notebook, or in the terminal.

pip install plotly

I won’t do a comprehensive overview, but I can show you a couple of examples. We’ll look at Plotly Express.

Let’s start by getting our stock data back in.

# Set-up

import numpy as np
import pandas as pd

# This brings in all of matplotlib
import matplotlib as mpl 

# This lets us refer to the pyplot part of matplot lib more easily. Just use plt!
import matplotlib.pyplot as plt

# Bring in Plotly Express
import plotly.express as px

# Bring in Plotly graphic objects
import plotly.graph_objects as go

import plotly.offline as py

# Keeps warnings from cluttering up our notebook. 
import warnings
warnings.filterwarnings('ignore')

# Include this to have plots show up in your Jupyter notebook.
%matplotlib inline 

# Read in some eod prices
stocks = pd.read_csv('https://raw.githubusercontent.com/aaiken1/fin-data-analysis-python/main/data/tr_eikon_eod_data.csv',
                  index_col=0, parse_dates=True)  

stocks.dropna(inplace=True)  

from janitor import clean_names

stocks = clean_names(stocks)

stocks.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2138 entries, 2010-01-04 to 2018-06-29
Data columns (total 12 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   aapl_o  2138 non-null   float64
 1   msft_o  2138 non-null   float64
 2   intc_o  2138 non-null   float64
 3   amzn_o  2138 non-null   float64
 4   gs_n    2138 non-null   float64
 5   spy     2138 non-null   float64
 6   _spx    2138 non-null   float64
 7   _vix    2138 non-null   float64
 8   eur=    2138 non-null   float64
 9   xau=    2138 non-null   float64
 10  gdx     2138 non-null   float64
 11  gld     2138 non-null   float64
dtypes: float64(12)
memory usage: 217.1 KB

Let’s make a simple line graph of prices for Apple. As a reminder, our date is our index in this DataFrame.

fig = px.line(stocks, x=stocks.index, y='aapl_o', title='Apple Price History')
fig.show()
/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.

/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.

Not bad! You can create what plotly calls graphic objects. You than add traces, similar to axes, and start to layer things together. Here, we’ll create our “blank” figure and then add three more price sequences.

# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=stocks.index, y=stocks.aapl_o,
                    mode='lines',
                    name='AAPL'))
fig.add_trace(go.Scatter(x=stocks.index, y=stocks.msft_o,
                    mode='lines',
                    name='MSFT'))
fig.show()
/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.

/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.

We can create a histogram of returns too. I added a rug on the top which helps you see the distribution and outliers better. I am also showing the percentage of observations in a bin, not a count.

And, I made a bunch of other changes, just to give you an idea of the syntax.

stocks['aapl_ret'] = np.log(stocks.aapl_o / stocks.aapl_o.shift(1))  

fig = px.histogram(stocks, x='aapl_ret', 
                   marginal="rug", # That thing at the top!
                   histnorm='percent', 
                   opacity=0.75, # alpha
                   width=600, #pixels
                   height=400,
                   template="simple_white")

fig.update_layout(
    title_text='Apple Return Distribution', # title of plot
    xaxis_title_text='Return', # xaxis label
    yaxis_title_text='Percent', # yaxis label
    bargap=0.2, # gap between bars of adjacent location coordinates
    bargroupgap=0.1 # gap between bars of the same location coordinates
)


fig.show()
/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.

/opt/anaconda3/lib/python3.9/site-packages/plotly/io/_renderers.py:395: DeprecationWarning:

distutils Version classes are deprecated. Use packaging.version instead.