quantopian / pyfolio

Portfolio and risk analytics in Python
https://quantopian.github.io/pyfolio
Apache License 2.0
5.72k stars 1.78k forks source link

Unable to display date as x-axis when using "create_returns_tear_sheet" #672

Open jackylawtrading opened 3 years ago

jackylawtrading commented 3 years ago

Problem Description

I am not able to display the date as x-axis when using "create_returns_tear_sheet". All of the plots can only display their y-labels but not for x-labels (blank there).

Please see the following codes. I used it to generate the plots:

import pandas as pd
import numpy as np
import pyfolio as pf
import matplotlib.pyplot as plt
%matplotlib inline 
import warnings
warnings.filterwarnings('ignore')

df = pd.read_excel("index.xlsx")
df = df.fillna(0)

df = df[df['daily ret']!=0]
len(df) # double checked the removal of rows with daily ret as zero is correct

df['Date'] = pd.to_datetime(df['Date'],format='%Y-%m-%d')

df=df[['Date','daily ret']]
df.set_index('Date', inplace=True)
returns = df['daily ret']

pf.create_returns_tear_sheet(returns,
                            positions=None,
                            transactions=None,
                            cone_std=(1.0,1.5,2.0),
                            benchmark_rets=None,
                            bootstrap=False,
                            header_rows=None,
                            return_fig=False)
slps20425 commented 3 years ago

same here, any solution?

Biszu commented 3 years ago

Removing every sharex=ax_rolling_returns from def create_returns_tear_sheet fixed this for me.

sometxdude commented 3 years ago

I am having the same issue.

yitelee commented 3 years ago

You can downgrade matplotlib. pip install matplotlib==2.2.5

Or patch it as below, by following the wisdom of : https://stackoverflow.com/questions/51553545/matplotlib-tick-labels-disappeared-after-set-sharex-in-subplots

stock_rets = pf.utils.get_symbol_rets('FB')
fig = pf.create_returns_tear_sheet(stock_rets, return_fig=True)

for ax in fig.axes:
        ax.tick_params(
        axis='x',           # changes apply to the x-axis
        which='both',       # both major and minor ticks are affected
        bottom=True,
        top=False,
        labelbottom=True)    # labels along the bottom edge are on

(tested with matplotlib: 3.4.2)