0

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:

  1. Start with a plot without legend with figsize = (8,8). This is not a problem of course.

  2. 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

  3. 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 option pad and rect 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")
  • What is `ax_sub`? In any case, if initially you had a figure with 10% margins (`m=0.1`) and width `w`, and then change the figure width by a factor `x`, `neww=x*w`, then the new margin needs to be `newm = 0.5-(1-2*m)/(2*x)`. Then `plt.subplots_adjust(left=newm, right=1-newm)` set this new margin. – ImportanceOfBeingErnest Sep 03 '19 at 11:33
  • It was a copy-paste error. I decided to change the process. I started with a big rectangular figure and then I have shrinked the image with ```plt.subplots_adjust(left=newm, right=1-newm)```. In this way there is enough space for the legend and when shirnked the image goes from rectangular to being squared. Unfortunately ```plt.subplots_adjust(left=newm, right=1-newm)``` shrinks – BalrogOfMoria Sep 03 '19 at 13:37
  • Isn't shrinking the image compared to the increased figure size what you want to do? So the above should actually do what you need. (The code in the question is no [mcve], so I cannot give a full working answer here.) – ImportanceOfBeingErnest Sep 03 '19 at 14:54

0 Answers0