toom
toom

Reputation: 13316

How do I extend the margin at the bottom of a figure in Matplotlib?

The following screenshot shows my x-axis.

enter image description here

I added some labels and rotated them by 90 degrees in order to better read them. However, pyplot truncates the bottom such that I'm not able to completely read the labels. How do I extend the bottom margin in order to see the complete labels?

Upvotes: 75

Views: 94214

Answers (6)

Paul H
Paul H

Reputation: 68146

Two retroactive ways:

fig, ax = plt.subplots()
# ...
fig.tight_layout()

Or

fig.subplots_adjust(bottom=0.2) # or whatever

Here's a subplots_adjust example: http://matplotlib.org/examples/pylab_examples/subplots_adjust.html

(but I prefer tight_layout)

Upvotes: 96

Vince Hall
Vince Hall

Reputation: 44

This is rather complicated, but it gives a general and neat solution.

import numpy as np
value1 = 3

xvalues = [0, 1, 2, 3, 4]
line1 = [2.0, 3.0, 2.0, 5.0, 4.0]
stdev1 = [0.1, 0.2, 0.1, 0.4, 0.3]

line2 = [1.7, 3.1, 2.5, 4.8, 4.2]
stdev2 = [0.12, 0.18, 0.12, 0.3, 0.35]

max_times = [max(line1+stdev1),max(line2+stdev2)]
min_times = [min(line1+stdev1),min(line2+stdev2)]

font_size = 25

max_total = max(max_times)
min_total = min(min_times)

max_minus_min = max_total - min_total

step_size = max_minus_min/10
head_space = (step_size*3) 


plt.figure(figsize=(15, 15))
plt.errorbar(xvalues, line1, yerr=stdev1, fmt='', color='b')

plt.errorbar(xvalues, line2, yerr=stdev2, fmt='', color='r')
plt.xlabel("xvalues", fontsize=font_size)
plt.ylabel("lines 1 and 2 Test "+str(value1), fontsize=font_size)
plt.title("Let's leave space for the legend Experiment"+ str(value1), fontsize=font_size)
plt.legend(("Line1", "Line2"), loc="upper left", fontsize=font_size)
plt.tick_params(labelsize=font_size)
plt.yticks(np.arange(min_total, max_total+head_space, step=step_size) )
plt.grid()
plt.tight_layout()

Result: Plot with headspace for the legend, big enough font, gridlines.

Upvotes: 0

Marcel
Marcel

Reputation: 285

fig.savefig('name.png', bbox_inches='tight')

works best for me, since it doesn't reduce the plot size compared to

fig.tight_layout()

Upvotes: 15

chinnychinchin
chinnychinchin

Reputation: 5674

A quick one-line solution that has worked for me is to use pyplot's auto tight_layout method directly, available in Matplotlib v1.1 onwards:

plt.tight_layout()

This can be invoked immediately before you show the plot (plt.show()), but after your manipulations on the axes (e.g. ticklabel rotations, etc).

This convenience method avoids manipulating individual figures of subplots.

Where plt is the standard pyplot from: import matplotlib.pyplot as plt

Upvotes: 27

Youjun Hu
Youjun Hu

Reputation: 1324

fig, ax = plt.subplots(tight_layout=True)

Upvotes: 1

rainer
rainer

Reputation: 3411

Subplot-adjust did not work for me, since the whole figure would just resize with the labels still out of bounds.

A workaround I found was to keep the y-axis always a certain margin over the highest or minimum y-values:

x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,y1 - 100 ,y2 + 100))

Upvotes: 10

Related Questions