Gonzalo
Gonzalo

Reputation: 1114

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.

Upvotes: 26

Views: 28726

Answers (1)

James
James

Reputation: 36756

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)

Upvotes: 40

Related Questions