Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do you plot results from pipeline in research

How do you plot the outputs of the results in run_pipeline?

I want to plot the output in results to include the moving averages along with the close prices.

Example code below:

from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline, CustomFilter
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import Q1500US
import quantopian.pipeline.factors as pf
from quantopian.pipeline.filters import StaticAssets

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

securities = symbols(["NVDA", "AMZN"])
assets = StaticAssets(securities)

close = pf.Latest(
inputs=[USEquityPricing.close],
mask = assets)

sma_30 = pf.SimpleMovingAverage(
inputs=[USEquityPricing.close],
window_length=30,
mask = assets)

sma_60 = pf.SimpleMovingAverage(
inputs=[USEquityPricing.close],
window_length=60,
mask = assets)

ma_cross_up = sma_30>sma_60
ma_cross_down = sma_30<sma_60

pipe = Pipeline()
pipe.add(close, 'close')
pipe.add(sma_30,'sma_30')
pipe.add(sma_60,'sma_60')
pipe.add(ma_cross_up, 'ma_cross_up')
pipe.add(ma_cross_down, 'ma_cross_down')
pipe.set_screen(assets)

results = run_pipeline(pipe, start_date='2018-01-01', end_date='2018-03-01')

1 response

Pipelines return a pandas dataframe. Pandas makes plotting very simple. No need to import pyplot unless one wants more functionality. Simply use the 'plot' method. The dataframe has a multi-index. Level 1 are the securities. To get data for a single security I like to use the '.xs' method. Something like this will plot all the columns for all the dates returned for NVDA:

nvda_results = results.xs(symbols('NVDA'), level=1)  
nvda_results.plot()

That will be a pretty basic plot but is often good for a quick look. There are a lot of other options for plotting. Take a look at the pandas documentation http://pandas.pydata.org/pandas-docs/version/0.18.0/visualization.html

Now, a BIG gotcha with pipeline data is that it is NOT split or dividend adjusted. The data is exactly what one would have seen on a specific date. This is good for some analysis but not so good for others. If one wants split adjusted data then use the 'get_pricing' method. That method also returns a dataframe and the output data can be plotted in a similar fashion.

See attached notebook (towards the end of the notebook is an example of how non-adjusted pipeline data can be misleading).

Good luck.