devnull
devnull

Reputation: 147

Superfluous white when using matplotlib pcolor

I'm trying to plot a 101x145 matrix as a heatmap using matplotlib. However, there is superfluous white on the top and right of the plot. How do I get rid of it?

Consider the following example:

import cPickle as pickle
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.pcolor(np.zeros((101,145)))
plt.show()

Upvotes: 1

Views: 145

Answers (1)

Francesco Montesano
Francesco Montesano

Reputation: 8658

fig.tight_layout does what you want. It accounts for all the subplots, axis labels, axis ticklables, etc. and try to stretch what is plotted in order to leave as less empty space as possible. You can also decide the padding and the box, in figure coordinates, within which tight_layout should work.

Here are some examples.

A word of warning: tight_layout works only with subplots not with Axes objects

Upvotes: 1

Related Questions