Skip to content Skip to sidebar Skip to footer

Remove Whitespace From Matplotlib Heatplot

I have a heatplot in matplotlib for which I want to remove the whitespace to the north and east of the plot, as shown in the image below. here is the code I'm using to generate th

Solution 1:

The easiest way to do this is to use ax.axis('tight').

By default, matplotlib tries to choose "even" numbers for the axes limits. If you want the plot to be scaled to the strict limits of your data, use ax.axis('tight'). ax.axis('image') is similar, but will also make the cells of your "heatmap" square.

For example:

import numpy as np
import matplotlib.pyplot as plt

# Note the non-"even" size... (not a multiple of 2, 5, or 10)
data = np.random.random((73, 78))

fig, axes = plt.subplots(ncols=3)
for ax, title in zip(axes, ['Default', 'axis("tight")', 'axis("image")']):
    ax.pcolormesh(data)
    ax.set(title=title)

axes[1].axis('tight')
axes[2].axis('image')

plt.show()

enter image description here

Post a Comment for "Remove Whitespace From Matplotlib Heatplot"