0

I am doing a Portfolio analysis. I plot the portfolio returns in a Graph. I could make a figure with 5 years, each years having its own graph, with 2 portfolios in each graph. I have then 5 subplots in a figure. For each subplots I have 2 lines, one for each portfolio, that are the referring to the same for each subplot (see image). So I wanted to have only one legend for the whole figure.

My results

However, with my tries, it always returns this error: 'Figure' object has no attribute 'figlegend'

I have tried 2 possibilities (that are shown on the matplotlib website) to make a figlegend, these are the following:

periods= [0,60,120,180,240,300,360,420,480,540,600,660,720,780,840,900]



fig, axis = plt.subplots(5, 1)
fig.set_size_inches(15, 20)
fig.tight_layout(pad=5.0)
for i in range(5):
    p = i+0 
    axis[i].plot(return_pf_market[periods[p]:periods[p+1]], "darkcyan")
    axis[i].plot(return_pf_optimized[periods[p]:periods[p+1]], "lightgreen")
    axis[i].set_title(year_str[p])
    axis[i].set_xlabel(year_TR[p]+" to "+ year_TR[p+4])
fig.figlegend(['Standard Portfolio', 'Green Portfolio'])
plt.show()

OR

fig, axis = plt.subplots(5, 1)
fig.set_size_inches(15, 20)
fig.tight_layout(pad=5.0)
for i in range(5):
    p = i+0 
    axis[i].plot(return_pf_market[periods[p]:periods[p+1]], "darkcyan", label='Standard Portfolio')
    axis[i].plot(return_pf_optimized[periods[p]:periods[p+1]], "lightgreen",  label='Green Portfolio')
    axis[i].set_title(year_str[p])
    axis[i].set_xlabel(year_TR[p]+" to "+ year_TR[p+4])
axis.figlegend()
plt.show()

Do maybe somebody know what is wrong in my code, that makes me get this error ?

  • I think what you're looking for is just fig.legend() and not "figlegend". This has been answered nicely here: https://stackoverflow.com/questions/9834452/how-do-i-make-a-single-legend-for-many-subplots – pyrifo Nov 11 '22 at 09:01

0 Answers0