Skip to content

Commit 3ce1427

Browse files
committed
Fix a bug in reports functions
1 parent 465d3e4 commit 3ce1427

File tree

8 files changed

+163
-113
lines changed

8 files changed

+163
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ If you use Riskfolio-Lib for published work, please use the following BibTeX ent
262262
```
263263
@misc{riskfolio,
264264
author = {Dany Cajas},
265-
title = {Riskfolio-Lib (6.3.0)},
265+
title = {Riskfolio-Lib (6.3.1)},
266266
year = {2024},
267267
url = {https://github.com/dcajasn/Riskfolio-Lib},
268268
}

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
copyright = '2020-2024, Dany Cajas'
2626
author = 'Dany Cajas'
2727

28-
__version__ = "6.3.0"
28+
__version__ = "6.3.1"
2929

3030
# The short X.Y version
3131
version = '.'.join(__version__.split('.')[:2])

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ If you use Riskfolio-Lib for published work, please use the following BibTeX ent
310310

311311
@misc{riskfolio,
312312
author = {Dany Cajas},
313-
title = {Riskfolio-Lib (6.3.0)},
313+
title = {Riskfolio-Lib (6.3.1)},
314314
year = {2024},
315315
url = {https://github.com/dcajasn/Riskfolio-Lib},
316316
}

examples/Tutorial 16 - Riskfolio-Lib Reports in Jupyter Notebook and Excel.ipynb

Lines changed: 73 additions & 70 deletions
Large diffs are not rendered by default.

examples/report.xlsx

-321 Bytes
Binary file not shown.

riskfolio/src/PlotFunctions.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import matplotlib.lines as mlines
1515
import matplotlib.ticker as mticker
1616
from matplotlib import cm, colors
17-
from matplotlib.gridspec import GridSpec
17+
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
1818
import scipy.stats as st
1919
import scipy.cluster.hierarchy as hr
2020
from scipy.spatial.distance import squareform
@@ -452,7 +452,12 @@ def plot_frontier(
452452
if isinstance(ax, plt.Axes):
453453
ax.axis("off")
454454
fig = ax.get_figure()
455-
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios)
455+
if hasattr(ax, 'get_subplotspec'):
456+
subplot_spec = ax.get_subplotspec()
457+
gs0 = subplot_spec.get_gridspec()
458+
gs = GridSpecFromSubplotSpec(nrows=1, ncols=2, width_ratios=width_ratios, subplot_spec=gs0[0])
459+
else:
460+
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios)
456461
axes = []
457462
axes.append(fig.add_subplot(gs[0]))
458463
axes.append(fig.add_subplot(gs[1]))
@@ -2274,7 +2279,12 @@ def plot_drawdown(
22742279
if isinstance(ax, plt.Axes):
22752280
ax.axis("off")
22762281
fig = ax.get_figure()
2277-
gs = GridSpec(nrows=2, ncols=1, figure=fig, height_ratios=height_ratios)
2282+
if hasattr(ax, 'get_subplotspec'):
2283+
subplot_spec = ax.get_subplotspec()
2284+
gs0 = subplot_spec.get_gridspec()
2285+
gs = GridSpecFromSubplotSpec(nrows=2, ncols=1, height_ratios=height_ratios, subplot_spec=gs0[0])
2286+
else:
2287+
gs = GridSpec(nrows=2, ncols=1, figure=fig, height_ratios=height_ratios)
22782288
axes = []
22792289
axes.append(fig.add_subplot(gs[0]))
22802290
axes.append(fig.add_subplot(gs[1]))
@@ -2838,13 +2848,22 @@ def plot_clusters(
28382848
fig = ax.get_figure()
28392849
if dendrogram == True:
28402850
if isinstance(ax, plt.Axes):
2841-
gs = GridSpec(
2842-
nrows=2,
2843-
ncols=3,
2844-
figure=fig,
2845-
height_ratios=height_ratios_1,
2846-
width_ratios=width_ratios_1,
2847-
)
2851+
if hasattr(ax, 'get_subplotspec'):
2852+
subplot_spec = ax.get_subplotspec()
2853+
gs0 = subplot_spec.get_gridspec()
2854+
gs = GridSpecFromSubplotSpec(nrows=2,
2855+
ncols=3,
2856+
height_ratios=height_ratios_1,
2857+
width_ratios=width_ratios_1,
2858+
subplot_spec=gs0[0]
2859+
)
2860+
else:
2861+
gs = GridSpec(nrows=2,
2862+
ncols=3,
2863+
figure=fig,
2864+
height_ratios=height_ratios_1,
2865+
width_ratios=width_ratios_1,
2866+
)
28482867
axes = []
28492868
for i in range(2):
28502869
for j in range(3):
@@ -2853,7 +2872,20 @@ def plot_clusters(
28532872
raise TypeError("ax must be a matplotlib axes object.")
28542873
else:
28552874
if isinstance(ax, plt.Axes):
2856-
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios_2)
2875+
if hasattr(ax, 'get_subplotspec'):
2876+
subplot_spec = ax.get_subplotspec()
2877+
gs0 = subplot_spec.get_gridspec()
2878+
gs = GridSpecFromSubplotSpec(nrows=1,
2879+
ncols=2,
2880+
width_ratios=width_ratios_2,
2881+
subplot_spec=gs0[0]
2882+
)
2883+
else:
2884+
gs = GridSpec(nrows=1,
2885+
ncols=2,
2886+
figure=fig,
2887+
width_ratios=width_ratios_2,
2888+
)
28572889
axes = []
28582890
for i in range(1):
28592891
for j in range(2):

riskfolio/src/Reports.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import pandas as pd
1111
import matplotlib.pyplot as plt
12+
from matplotlib.gridspec import GridSpec
1213
from xlsxwriter.utility import xl_range_abs, xl_range, xl_rowcol_to_cell, xl_col_to_name
1314
import datetime
1415
import riskfolio.src.PlotFunctions as plf
@@ -21,7 +22,7 @@
2122
]
2223

2324

24-
__LICENSE__ = """Copyright (c) 2020-2023, Dany Cajas
25+
__LICENSE__ = """Copyright (c) 2020-2024, Dany Cajas
2526
All rights reserved.
2627
2728
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
@@ -194,13 +195,26 @@ def jupyter_report(
194195

195196
cov = returns.cov()
196197

197-
fig, ax = plt.subplots(
198-
nrows=6,
199-
figsize=(width, height * 6),
200-
gridspec_kw={"height_ratios": [2, 1, 1.5, 1, 1, 1]},
201-
)
198+
fig, ax = plt.subplots(figsize=(width, height * 6))
199+
ax.axis('off')
200+
201+
gs0 = GridSpec(5, 1, figure=fig, height_ratios=[2, 1.5, 1, 2, 1])
202+
gs00 = gs0[0].subgridspec(1, 1)
203+
ax0 = fig.add_subplot(gs00[0, 0])
204+
205+
gs01 = gs0[1].subgridspec(1, 1)
206+
ax1 = fig.add_subplot(gs01[0, 0])
207+
208+
gs02 = gs0[2].subgridspec(1, 1)
209+
ax2 = fig.add_subplot(gs02[0, 0])
202210

203-
ax[0] = plf.plot_table(
211+
gs03 = gs0[3].subgridspec(1, 1)
212+
ax3 = fig.add_subplot(gs03[0, 0])
213+
214+
gs04 = gs0[4].subgridspec(1, 1)
215+
ax4 = fig.add_subplot(gs04[0, 0])
216+
217+
ax0 = plf.plot_table(
204218
returns,
205219
w,
206220
MAR=rf,
@@ -211,19 +225,34 @@ def jupyter_report(
211225
t_factor=t_factor,
212226
ini_days=ini_days,
213227
days_per_year=days_per_year,
214-
ax=ax[0],
228+
ax=ax0,
215229
)
216230

217-
ax[2] = plf.plot_pie(
231+
ax1 = plf.plot_pie(
218232
w=w,
219233
title="Portfolio Composition",
220234
others=others,
221235
nrow=nrow,
222236
cmap=cmap,
223-
ax=ax[2],
237+
ax=ax1,
238+
)
239+
240+
ax2 = plf.plot_hist(
241+
returns=returns,
242+
w=w,
243+
alpha=alpha,
244+
a_sim=a_sim,
245+
kappa=kappa,
246+
solver=solver,
247+
bins=bins,
248+
ax=ax2,
249+
)
250+
251+
ax3 = plf.plot_drawdown(
252+
returns=returns, w=w, alpha=alpha, kappa=kappa, solver=solver, ax=ax3
224253
)
225254

226-
ax[3] = plf.plot_risk_con(
255+
ax4 = plf.plot_risk_con(
227256
w=w,
228257
cov=cov,
229258
returns=returns,
@@ -240,22 +269,7 @@ def jupyter_report(
240269
erc_line=erc_line,
241270
color=color,
242271
erc_linecolor=erc_linecolor,
243-
ax=ax[3],
244-
)
245-
246-
ax[4] = plf.plot_hist(
247-
returns=returns,
248-
w=w,
249-
alpha=alpha,
250-
a_sim=a_sim,
251-
kappa=kappa,
252-
solver=solver,
253-
bins=bins,
254-
ax=ax[4],
255-
)
256-
257-
ax[[1, 5]] = plf.plot_drawdown(
258-
returns=returns, w=w, alpha=alpha, kappa=kappa, solver=solver, ax=ax[[1, 5]]
272+
ax=ax4,
259273
)
260274

261275
year = str(datetime.datetime.now().year)
@@ -264,7 +278,7 @@ def jupyter_report(
264278
subtitle = "Copyright (c) 2020-" + year + ", Dany Cajas. All rights reserved."
265279

266280
fig.suptitle(title, fontsize="xx-large", y=1.011, fontweight="bold")
267-
ax[0].set_title(subtitle, fontsize="large", ha="center", pad=10)
281+
ax0.set_title(subtitle, fontsize="large", ha="center", pad=10)
268282

269283
return ax
270284

@@ -349,6 +363,7 @@ def excel_report(
349363
n2 = returns.shape[0]
350364

351365
portfolios = w.columns.tolist()
366+
returns.index = returns.index.tz_localize(None)
352367
dates = returns.index.tolist()
353368
year = str(datetime.datetime.now().year)
354369
days = (returns.index[-1] - returns.index[0]).days + ini_days

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
MAJOR = 6
1010
MINOR = 3
11-
MICRO = 0
11+
MICRO = 1
1212
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
1313

1414
def write_version_py(filename='riskfolio/version.py'):

0 commit comments

Comments
 (0)