Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_12/python-notebooks-data-wrangling/Visualization--Multiples-Variety.ipynb
1904 views
Kernel: Python 3
%matplotlib inline from pathlib import Path import matplotlib.pyplot as plt import pandas as pd DATA_DIR = Path('data', 'financial', 'raw') TICKERS = ['AMZN', 'FB', 'GOOG', 'MSFT']
xlist = [] for ticker in TICKERS: fname = str(DATA_DIR.joinpath(ticker + '.csv')) xf = pd.read_csv(fname, parse_dates=['Date']) xf['company'] = ticker xf = xf.sort_values('Date', ascending=True) # http://pandas.pydata.org/pandas-docs/stable/indexing.html#the-in-and-not-in-operators xf = xf[xf['Date'].dt.year.isin([2014, 2015, 2016])] # make a new dataframe, trimmed down xf = xf[['company', 'Date', 'Adj Close']] xf['pct_change'] = xf['Adj Close'].pct_change() * 100 xlist.append(xf) # a list of dataframes, nothing more
# make one big frame df = pd.concat(xlist, ignore_index=True)
df.head()

Matplotlib all in one

fig, ax = plt.subplots(figsize=(20, 10)) for ticker in TICKERS: xf = df[df['company'] == ticker] ax.plot(xf['Date'], xf['Adj Close']) ax.legend(TICKERS)
<matplotlib.legend.Legend at 0x10b471710>
Image in a Jupyter notebook

Matplotlib subplots

nrow = len(TICKERS) fig, axlist = plt.subplots(1, nrow, sharey=True, figsize=(20, 5)) for n in range(nrow): ticker = TICKERS[n] xf = df[df['company'] == ticker] ax = axlist[n] ax.plot(xf['Date'], xf['Adj Close']) ax.set_title(ticker)
Image in a Jupyter notebook
nrow = len(TICKERS) fig, axlist = plt.subplots(1, nrow, sharey=True, figsize=(20, 5)) for n in range(nrow): ticker = TICKERS[n] xf = df[df['company'] == ticker] ax = axlist[n] ax.plot(xf['Date'], xf['pct_change']) ax.set_title(ticker)
Image in a Jupyter notebook