Remove whitespace from matplotlib heatplot

后端 未结 1 1646
一个人的身影
一个人的身影 2021-01-22 16:39

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.

相关标签:
1条回答
  • 2021-01-22 16:47

    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

    0 讨论(0)
提交回复
热议问题