Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 829
toom Avatar asked Jan 10 '15 16:01

toom


People also ask

How do I add margins in Matplotlib?

Set the title of the plot. Set margins of the plot using margins(x=0, y=0). To display the figure, use show() method.

How do I increase white space in Matplotlib?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.

What is padding in Matplotlib?

pad: This parameter is used for padding between the figure edge and the edges of subplots, as a fraction of the font size. h_pad, w_pad: These parameter are used for padding (height/width) between edges of adjacent subplots, as a fraction of the font size.


2 Answers

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)

like image 112
Paul H Avatar answered Sep 29 '22 12:09

Paul H


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

like image 29
chinnychinchin Avatar answered Sep 29 '22 14:09

chinnychinchin