This notebook shows how to plot multiindexed data (like the pipeline output for multiple assets) in three different ways.
import numpy as np
import matplotlib.pyplot as plt
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.pipeline.data import Fundamentals
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import StaticAssets
from quantopian.research import symbols
from quantopian.pipeline.data import USEquityPricing
import matplotlib.pyplot as plt
import pandas as pd
assets_screen = StaticAssets(symbols(['NKE', 'UAA']))
fundamentals = {
'total_assets': Fundamentals.total_assets.latest,
'total_liabilities': Fundamentals.total_liabilities.latest,
'market_cap': Fundamentals.market_cap.latest
}
my_pipeline = Pipeline(columns=fundamentals, screen=assets_screen)
data = run_pipeline(my_pipeline, '2002-1-01', '2018-6-27', 90)
data.head()
# Slice dataframe to get values for each stock
idx = pd.IndexSlice
data_1 = data.loc[idx[:, symbols('NKE')], :]
data_2 = data.loc[idx[:, symbols('UAA')], :]
# This constructs the same plot, but using cleaner code
columns = list(data.columns)
dates = data.index.levels[0]
# Iterate through columns and plot each one
for column in columns:
plt.plot(dates, data_1[column], label='NKE '+str(column))
plt.plot(dates, data_2[column], label='UAA '+str(column))
plt.legend()
plt.title('NKE & UAA Balance Sheet Comparison (in 100s of billions)')
plt.show()
import qgrid
# Get data in Qgrid
qgrid_widget = qgrid.show_grid(data)
qgrid_widget
# Filter the second index column to show only rows for T,
# then save the changed dataframe
T_data = qgrid_widget.get_changed_df()
# Filter the second index column to show only rows for VZ,
# then save the changed dataframe
VZ_data = qgrid_widget.get_changed_df()
T_data.head()
VZ_data.head()
# Now plot using the same code from above
columns = list(data.columns)
dates = data.index.levels[0]
plt.clf() # clears previous plot
# Iterate through columns and plot each one
for column in columns:
plt.plot(dates, T_data[column], label='T '+str(column))
plt.plot(dates, VZ_data[column], label='VZ '+str(column))
plt.legend()
plt.title('Historical T CashFlow (in billions)')
plt.show()