I am doing a plot with a very big legend on the top of the figure following the last example of this link.
Since the font size of the legend must be very big, it happens that both on the right and on the left the legend is cropped.
What I would like to do is adding some extra whitespace on the left and on the right of my image in order to let the legend fit in. More formally, the process must be this:
Start with a plot without legend with figsize = (8,8). This is not a problem of course.
Add some extra whitespace on the left and on the right. This would bring the figsize to be rectangular e.g. (12, 8). The initial plot must be on the center and looking squared. That is the critical point
Add the horizontal legend on the plot that now doesn't get cropped since the figsize has more space on the two sides.
To solve this problem I have tried to:
Use
fig.tight_layout()
with the optionpad
andrect
as suggested by this link, but it didn't work for me.I could follow what this link suggests, but it doesn't fit with my needs because I can't reduce the font size of the legend and I can't change the figsize initally because otherwise the starting plot would look rectangular instead of squared.
I have tried to use
subplots_adjust(w_space)
, but the problem persists.
I let you with an example to reproduce my current situation:
fig, ax = plt.subplots(1, 1, figsize = (10,10))
ax.plot(range(5),range(0,20,4), label="pippo")
ax.plot(range(5),range(0,25,5), label="pappo")
ax.plot(range(5),range(0,25,5), label="tp")
ax.plot(range(5),range(0,25,5), label="minni")
ax.plot(range(5),range(0,25,5), label="pluto")
ax.plot(range(5),range(0,25,5), label="paperino")
ax.plot(range(5),range(0,25,5), label="chicho")
ax.plot(range(5),range(0,25,5), label="mela")
ax.plot(range(5),range(0,25,5), label="pera")
ax.plot(range(5),range(0,25,5), label="banana")
# Shrink current axis's height by 10% on the bottom
box = ax.get_position()
ax.set_position([box.x0, box.y0 - box.height * 0.1,
box.width, box.height * 0.9])
# Put a legend below current axis
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.2),
fancybox=True, shadow=True, ncol=10, prop={'size':32})
fig.savefig("check")