quantopian / alphalens

Performance analysis of predictive (alpha) stock factors
http://quantopian.github.io/alphalens
Apache License 2.0
3.18k stars 1.12k forks source link

Random empty figures when plotting tear sheets #370

Open Boubside opened 4 years ago

Boubside commented 4 years ago

Random empty figures when plotting tear sheets

The tears modules randomly plot empty figure when creating some of the tear sheets on a non interactive python environment.

Minimal example:

import alphalens
import datetime as dt
import pandas as pd

price = {'A': [76.930, 76.730, 76.850, 76.750, 76.750, 76.880, 76.925], 'AAL': [30.240, 30.210, 30.210, 30.200, 30.150, 30.245, 30.315]}
factor = {'A': [0,0,0,0], 'AAL': [0,0,-0.142857,0]}

pricings = pd.DataFrame.from_dict(price)
pricings['date'] = pd.date_range(dt.datetime(2019,11,28),dt.datetime(2019,11,28) + dt.timedelta(minutes=6), freq="1min")
pricings.set_index(['date'], inplace=True)

signal = pd.DataFrame.from_dict(factor)
signal['date'] = pd.date_range(dt.datetime(2019,11,28),dt.datetime(2019,11,28) + dt.timedelta(minutes=3), freq="1min")
signal.set_index(['date'], inplace=True)
signal = signal.stack()

print(pricings)
print(signal)

factor_data = alphalens.utils.get_clean_factor_and_forward_returns(signal,
                                                                   pricings,
                                                                   quantiles=None,
                                                                   bins = 2,
                                                                   periods=(1,2))

alphalens.tears.create_information_tear_sheet(factor_data)

This code will create two figures, with the first one being empty, even if only one is created in the create_information_tear_sheet method.

Fix: This can be easily fixed by adding plt.close() in the constructor of tears.GridFigure.

Versions