Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Matplotlib - Adjusting the spacing between the edge of the plot and the x-axis

How can I adjust the amount of space between the x-axis and the edge of the plot window? My x-axis labels are oriented vertically and they are running off of the edge of the window that Matplotlib draws.

Here's some example code:

import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[1,2,3,4,5]
plt.plot(x,y)
plt.xticks(rotation='vertical')
plt.show()
like image 235
thenickname Avatar asked Feb 22 '11 15:02

thenickname


2 Answers

As Paul said, you are using figures. You can get a reference to the current figure with gcf() and then set the spacing as per the FAQ. I've added two lines to your code:

import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[1,2,3,4,5]
plt.plot(x,y)
plt.xticks(rotation='vertical')

fig = plt.gcf()
fig.subplots_adjust(bottom=0.2)

plt.show()
like image 65
Daan Avatar answered Nov 19 '22 06:11

Daan


Here's a solution in the FAQ entitled Move the edge of an axes to make room for tick labels.

like image 31
Paul Avatar answered Nov 19 '22 05:11

Paul