# python – Why is my xlabel cut off in my matplotlib plot?

## The Question :

351 people think this question is useful

I am plotting a dataset using matplotlib where I have an xlabel that is quite “tall” (it’s a formula rendered in TeX that contains a fraction and is therefore has the height equivalent of a couple of lines of text).

In any case, the bottom of the formula is always cut off when I draw the figures. Changing figure size doesn’t seem to help this, and I haven’t been able to figure out how to shift the x-axis “up” to make room for the xlabel. Something like that would be a reasonable temporary solution, but what would be nice would be to have a way to make matplotlib recognize automatically that the label is cut off and resize accordingly.

Here’s an example of what I mean:

import matplotlib.pyplot as plt

plt.figure()
plt.ylabel(r'$\ln\left(\frac{x_a-x_b}{x_a-x_c}\right)$')
plt.xlabel(r'$\ln\left(\frac{x_a-x_d}{x_a-x_e}\right)$')
plt.show()



while you can see the entire ylabel, the xlabel is cut off at the bottom.

In the case this is a machine-specific problem, I am running this on OSX 10.6.8 with matplotlib 1.0.0

• You could post on an image hosting site and link it here.
• It helps if you can post a minimalistic sample code that triggers this issue. This way, people can understand and reproduce your problem faster, and they will be more likely to help you.
• Your code works just fine (display the formula fully visible) on my machine (ubuntu 11.04 64bit). Maybe is a machine-specific problem [like a font with wrong dimensional information being used in the image?]. You could perhaps specify the system you are using in your question.
• you may also want to try plt.savefig("test.png",bbox_inches='tight'): stackoverflow.com/questions/21288062/…

530 people think this answer is useful

Use:

import matplotlib.pyplot as plt



to make room for the label.

Edit:

Since i gave the answer, matplotlib has added the tight_layout() function. So i suggest to use it:

plt.tight_layout()



should make room for the xlabel.

168 people think this answer is useful

An easy option is to configure matplotlib to automatically adjust the plot size. It works perfectly for me and I’m not sure why it’s not activated by default.

Method 1

Set this in your matplotlibrc file

figure.autolayout : True



Method 2

Update the rcParams during runtime like this

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})



The advantage of using this approach is that your code will produce the same graphs on differently-configured machines.

149 people think this answer is useful

In case you want to store it to a file, you solve it using bbox_inches="tight" argument:

plt.savefig('myfile.png', bbox_inches = "tight")



9 people think this answer is useful

plt.autoscale() worked for me.

7 people think this answer is useful

Putting plot.tight_layout() after all changes on the graph, just before show() or savefig() will solve the problem.

5 people think this answer is useful

You can also set custom padding as defaults in your \$HOME/.matplotlib/matplotlib_rc as follows. In the example below I have modified both the bottom and left out-of-the-box padding:

# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
figure.subplot.left  : 0.1 #left side of the subplots of the figure
#figure.subplot.right : 0.9
figure.subplot.bottom : 0.15
...



df.plot(........,sharex=False)