Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge space between title and plot matplotlib

I've a issue with matplotlib that generates a plot(graph) very far from the title. My code is:

df = pandas.read_csv(csvpath, delimiter=',', 
                     index_col=0, 
                     parse_dates=[0], dayfirst=True, 
                     names=['Date','test1'])

df.plot(subplots=True, marker='.',markersize=8, title ="test", fontsize = 10, color=['b','g','g'], figsize=(8, 22))

imagepath = 'capture.png'

plt.savefig(imagepath)

An example of the graph generated is:

enter image description here

Any idea why this is happening? Thanks a lot.

like image 291
Gonzalo Avatar asked Sep 05 '16 12:09

Gonzalo


1 Answers

You can adjust the title location by grabbing the figure object from one of the 3 axis objects that is returned in an array from df.plot. Using fig.tight_layout removes extra whitespace, but a known problem is that is also sets the figure title within the top plot. However, you can fix that using the last line below.

ax = df.plot(subplots=True, marker='.', markersize=8, 
             title ="test", fontsize = 10, color=['b','g','g'], 
             figsize=(8, 22))[0] 
fig = ax.get_figure()
fig.tight_layout()
fig.subplots_adjust(top=0.95)
like image 71
James Avatar answered Nov 07 '22 05:11

James